python - BeautifulSoup 找不到包含内容的元标记
问题描述
我可以看到<meta data-react-helmet="true" name="twitter:title" content="Mamma Mia! Here We Go Again Is the Only Good Thing About This Summer - Vogue"/>
存在,但是此代码似乎无法正确获取此信息:
url = 'https://www.vogue.com/article/mamma-mia-2-here-we-go-again-review?mbid=social_twitter'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, "lxml")
title = soup.find("meta", {"name": "twitter:title"})
title2 = soup.find("meta", property="og:title")
title3 = soup.find("meta", property="og:description")
print("TITLE: "+str(title['content']))
print("TITLE2: "+str(title2['content']))
print("TITLE3: "+str(title3['content']))
输出:
File "twitscrape2.py", line 42, in <module>
print("TITLE: "+str(title['content']))
TypeError: 'NoneType' object has no attribute '__getitem__'
解决方案
推荐阅读
- flutter - Flutter:出现键盘时向上滚动屏幕
- python - 在 cloudshell (Google App Engine) 中何处查找已部署的应用程序文件?
- php - 无法在 EasyPHP Devserver 中更改 PHP 版本
- javascript - Nodejs - 如何通过快速响应发送 JSON 对象
- java - SpringBoot中如何将大文件上传到ftps服务器?
- c# - 如何在 C# 中检索占位符控件内动态下拉列表的选择值
- python - 拥有基于 pythod 的 FTDI 服务器的任何方式
- git - 如何将存储库的大量历史从一台服务器移动到另一台服务器?
- sql - BigQuery 跳过,用于数据网格分页
- mapbox - 如何在 Mapbox Studio 中创建带有方向线的样式?