我创建了一个代码来从网站中提取电子邮件:import requestsfrom bs4 import BeautifulSoupimport reurl = ""s = requests.Session()r = s.get(url, headers={"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"})soup = BeautifulSoup(r.content, 'html.parser')content = soup.get_text()emails_match = re.findall(r'[\w\.-]+@[\w\.-]+', content)它工作正常,但有时会从其他元素返回包含其他文本的电子邮件。print(email_match)['743-2538info@alliedsinterings.com']我只想获取电子邮件地址(没有来自其他 html 元素的任何文本)当我尝试另一个正则表达式时,它返回相同的内容,例如:r'([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+){0,}'
1 回答
胡说叔叔
TA贡献1804条经验 获得超8个赞
使用.strings而不是.text
import re
email = re.compile(r'([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+){0,}')
[x for x in soup.strings if email.search(x).group()]
['info@alliedsinterings.com']
添加回答
举报
0/150
提交
取消