Python BeautifulSoup未拾取元标记

Python BeautifulSoup未拾取元标记,python,beautifulsoup,meta,Python,Beautifulsoup,Meta,我有一个简单的脚本,它获取一个html页面,并尝试输出关键字的元标记的内容。不知何故,它没有拾取关键字元标记的内容,即使html包含该标记。感谢您的帮助 url = “https://www.mediapost.com/publications/article/316086/google-facebook-others-pitch-in-app-ads-brand-s.html” req = urllib2.Request(url=url) f = urllib2.url

我有一个简单的脚本,它获取一个html页面,并尝试输出关键字的元标记的内容。不知何故,它没有拾取关键字元标记的内容,即使html包含该标记。感谢您的帮助

    url = “https://www.mediapost.com/publications/article/316086/google-facebook-others-pitch-in-app-ads-brand-s.html”
    req = urllib2.Request(url=url)
    f = urllib2.urlopen(req)
    mycontent = f.read()
    soup = BeautifulSoup(mycontent, 'html.parser')
    keywords = soup.find("meta", property="keywords")
    print keywords

使用
'lxml'
而不是
'html.parser'
并使用
soup.find\u all

soup = BeautifulSoup(doc, 'lxml')
keywords = soup.find_all('meta',attrs={"name": 'keywords'})
for x in keywords:
    print(x['content'])
输出
如果您正确地检查了它,那么您要查找的元标记具有属性名称而不是属性,因此将代码更改为

keywords = soup.find("meta", attrs={'name':'keywords'})
然后显示您需要编写的内容

print keywords['content']
输出:

更多的主要品牌正在向手机游戏投入大量广告资金, 将谷歌、Facebook和其他公司推向应用程序内游戏广告空间。 有人认为这是对品牌寻求安全品牌的回应, 运行视频广告和与消费者接触的安全场所。2018年3月16日

我极力推荐你

代码:

from bs4 import BeautifulSoup
import requests

r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
keywords = soup.select_one('meta[name="keywords"]')['content']

但我只想提取一个meta标记的内容,其中meta name=“keywords”?已更新,
from bs4 import BeautifulSoup
import requests

r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
keywords = soup.select_one('meta[name="keywords"]')['content']
>>> keywords
'Many more major brands are pumping big ad dollars into mobile games, pushing Google, Facebook and others into the in-app gaming ad space. Some believe this is in response to brands searching for a secure, safe place to run video ads and engage with consumers. 03/16/2018'