【发布时间】:2018-05-10 22:01:59
【问题描述】:
我在 python 中编写了一个脚本来从网页中抓取电子邮件地址,但我无法做到。电子邮件地址位于script 标签内,我无法打破获取内容的障碍。任何帮助,我们将不胜感激。
到目前为止,我已经尝试过:
import requests
from bs4 import BeautifulSoup
url = "replace_with_link_above"
res = requests.get(url)
soup = BeautifulSoup(res.text, "lxml")
for items in soup.select(".profile-right-info"):
email = items.select_one("dd a[href^='mailto:']")['href']
print(email)
执行时出现以下错误:
email = items.select_one("dd a[href^='mailto:']")['href']
TypeError: 'NoneType' object is not subscriptable
顺便说一句,电子邮件链接位于该网页标题profile details 下的第二行。
【问题讨论】:
-
地址是受保护的,所以确保你得到了像 selemium 这样启用了 javascript 的东西,我假设你是用 urllib / requests 做到的。
items.select_one("dd a[href^='mailto:']")为 None 的错误信息。
标签: python python-3.x web-scraping beautifulsoup