Skip to main content
 首页 » 编程设计

python之BeautifulSoup 没有获取元标记

2025年01月19日10kuangbin

我有一个简单的脚本,它获取一个 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 

请您参考如下方法:

我强烈推荐你requests .

代码:

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'