【发布时间】:2021-04-19 18:18:37
【问题描述】:
我正在尝试从网页中抓取电子邮件地址。电子邮件地址在页面源 (ctrl + u) 中可用。但是,我仍然无法使用请求获取它。我得到的只是AttributeError。对此的任何帮助将不胜感激。
我目前的尝试:
import requests
from bs4 import BeautifulSoup
link = "https://www.facebook.com/pg/theultimatecollectionco/about/?ref=page_internal"
with requests.Session() as s:
s.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36'
r = s.get(link)
soup = BeautifulSoup(r.text,"lxml")
try:
email = soup.select_one("a[href^='mailto:']").get("href")
except AttributeError: email = ""
print(email)
【问题讨论】:
-
您是否查看过
r.text以确认存在所需的字段?如果网站使用javascript动态添加页面内容,requests模块是看不到的。
标签: python python-3.x web-scraping python-requests