【发布时间】:2020-03-25 21:54:36
【问题描述】:
我正在尝试使用 Requests 和 BeautifulSoup 的以下脚本来获取第一个具有 'og:image' 属性的元标记。
返回值应该是:
<meta property="og:image" content="https://images.metadata.sky.com/pd-image/ad81add2-dff2-4c4d-8596-277bb22c0905/16-9">
...但是,我不断得到:
<meta content="https://dm8eklel4s62k.cloudfront.net/images/sky-logo-b90e8c9.jpg" property="og:image"/>
链接https://dm8eklel4s62k.cloudfront.net/images/sky-logo-b90e8c9.jpg 实际上在相关页面的源代码中,但在 Javascript 响应中,而不是在页面标题元数据值中。奇怪的是,如果您将find 更改为findAll,则除此之外的所有其他元标记都是正确的。
有什么想法吗?
import requests
import beautiful soup
session = requests.Session()
jar = session.cookies
url ='https://www.sky.com/new-search/air-crash-investigation/?q=air%20crash%20investigation'
headers = {
'authority': 'www.sky.com',
'method': 'GET',
'path': url_split,
'scheme': 'https',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8',
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'none',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
}
params = {
'q': url_string2
}
r = session.get(url, headers=headers, params=params, cookies=jar)
soup = BeautifulSoup(r.text, "html.parser")
prop = soup.find('meta', attrs={'property': 'og:image'})
print prop
【问题讨论】:
-
您的第一步应该是切换到 Python 3! 2只剩下一个月左右了...
标签: python python-2.7 beautifulsoup python-requests