首页 > 解决方案 > BeautifulSoup 找不到元标记信息

问题描述

所有三个标题都返回“无”。但是,当我查看页面源时,我可以清楚地看到twitter:titleog:title并且og:description清楚地存在。

url = 'https://www.vox.com/culture/2018/8/3/17644464/christopher-robin-review-pooh-bear-winnie'
response = requests.get(url)

soup = BeautifulSoup(response.text, "lxml")

title = soup.find("meta",  property="twitter:title")
title2 = soup.find("meta",  property="og:title")
title3 = soup.find("meta",  property="og:description")

print("TITLE: "+str(title))
print("TITLE2: "+str(title2))
print("TITLE3: "+str(title3))

标签: pythonbeautifulsouppython-requests

解决方案


soup.find("meta", property="twitter:title")必须是soup.find("meta", {"name": "twitter:title"})(它是一个名称,而不是一个属性)。其他两行对我来说很好。


推荐阅读