python爬虫——正则获取手机号
生活随笔
收集整理的這篇文章主要介紹了
python爬虫——正则获取手机号
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
用正則匹配網頁手機號
1. 安裝re模塊、requests庫、beautifulsoup4庫
cmd → pip install re → 回車
cmd → pip install requests → 回車
cmd → pip install beautifulsoup4 → 回車
2.調用庫
import requests import re from bs4 import BeautifulSoup3. 調用網址
r = requests.get("http://www.haomahao.com/")4. 編寫正則表達式
parttern = "1[35789]\d{9}"5.完整代碼
import requests import re from bs4 import BeautifulSoupdef down():r = requests.get("http://www.haomahao.com/") #調用網頁r.encoding = r.apparent_encoding #更改網頁編碼,防止出現亂碼#print(r.text)bs = BeautifulSoup(r.text,"html.parser")str1 = bs.getText() #將BeautifulSoup提出出來的寫完textparttern = "1[35789]\d{9}" #編寫手機號的正則表達式list = re.findall(parttern,str1)#print(list) #調試正則是否正確f = open("手機號.txt",mode="w",encoding="utf-8") #寫入文件f.write("\n".join(list))f.close() if __name__ == "__main__":down()總結
以上是生活随笔為你收集整理的python爬虫——正则获取手机号的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 红心大战c语言程序设计,Win 7系统安
- 下一篇: 对激光原理的初步了解