I am a beginner of scraping who has just started scraping with Python. This time, make a note of how to extract only the numbers from the data extracted by scraping.
Extract only numbers from the data extracted by scraping
Extract only numbers using ** regular expressions **
Use the re module. Execute the following command from the terminal to install regex.
$ pip install regex
Next, let's import the ** re module ** into sample.py.
sample.py
import re
Now you are ready to use the re module.
sample.py
import re
dt = "regnkrnfskrngkaer gksgnkq laergnnktga3r erfkl 400 registrants aelrkna erglknnarg"
num = re.sub("\\D", "", dt)
print(num)
# 400
sample.py
import re
dt = ['regnkrnfskrngkaer', 'gksgnkq', 'laergnnktgar', 'erfkl', '400 registrants', 'aelrkna erglknnarg']
dt_str = ",".join(dt)
dt_num = re.sub("\\D", "", dt_str)
print(dt_num)
# 400
sample.py
import re
dt = ['regr111kaer', 'gg443nkq', 'laen56nktgar', 'er39fkl', '400 registrants', 'ael09narg']
dt_str = ",".join(dt)
dt_num = re.sub("\\D", "", dt_str)
print(dt_num)
# 111443563940009
sample.py
import re
dt = ['regr111kaer', 'gg443nkq', 'laen56nktgar', 'er39fkl', '400 registrants', 'ael09narg']
dt_pop = (dt.pop(4))
dt_num = re.sub("\\D", "", dt_pop)
print(dt_num)
# 400
reference https://qiita.com/sakamossan/items/161db7418ade037f6f3d https://qiita.com/ikanamazu/items/ba2a32a1a5924f3bd8e9
Recommended Posts