这里使用requests库爬取网页要比urllib库方便
用finditer查询
import requests
import re
url='https://www.flyai.com/' # 带爬取的网页
html=requests.get(url).text # text为转化为str数据
pat='(\w+@\w+.com)|(\d{11})' # 正则模式
res=re.finditer(pat,html) # finditer返回的是一个迭代器
for i in res: # i 是一个Match对象
print(i.group()) # group()为输出所有组
用findall查询
import requests
import re
url='https://www.flyai.com/'
html=requests.get(url).text
pat='(?:[a-zA-Z_-]+@[a-zA-Z_-]+.com)|(?:\d{11})'
res=re.findall(pat,html) # 返回的结果在一个列表中
print(res)

import requests
import re
url='https://www.sina.com.cn/'
html=requests.get(url).text
pat='[a-zA-Z_-]+@[a-zA-Z_-]+.\w*'
res=set(re.findall(pat,html))
print(res)

本文介绍如何利用Python的requests库获取网页内容,并通过正则表达式匹配和提取电子邮件地址。演示了finditer和findall两种查询方法,展示了从不同网站抓取邮箱的具体实现。
2920

被折叠的 条评论
为什么被折叠?



