我是靠谱客的博主 斯文黑裤,这篇文章主要介绍python爬虫——正则获取手机号,现在分享给大家,希望可以做个参考。

用正则匹配网页手机号

1. 安装re模块、requests库、beautifulsoup4库

cmd → pip install re → 回车
cmd → pip install requests → 回车
cmd → pip install beautifulsoup4 → 回车

2.调用库

复制代码
1
2
3
4
import requests import re from bs4 import BeautifulSoup

3. 调用网址

复制代码
1
2
r = requests.get("http://www.haomahao.com/")

4. 编写正则表达式

复制代码
1
2
parttern = "1[35789]d{9}"

5.完整代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import requests import re from bs4 import BeautifulSoup def 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提出出来的写完text parttern = "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爬虫——正则获取手机号的全部内容,更多相关python爬虫——正则获取手机号内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部