【发布时间】:2022-03-09 18:13:49
【问题描述】:
我正在尝试抓取网站元描述的内容。
示例:
<meta name="description" content="This is the home page meta description.">
我正在寻找的输出是:“这是主页元描述。”
我的代码是:
raw_html = simple_get(companyUrl)
html = BeautifulSoup(raw_html, 'html.parser')
x = html.select('meta', {'name' : 'description'}) ## this line errors out
有人能指出正确的方向吗?
(也是 - 是我的想象,还是 BeautifulSoup 教程/文档达不到其他语言/应用程序的水平?)
【问题讨论】:
-
您将
.select()与.find_all()混淆了。使用find_all或更改您的选择器。 -
@t.m.adam - 我想这就是我要问的:如何格式化我的选择器以捕获 tag=meta 和 name=description?
-
好的 - 我已经蛮力解决了,但我希望有一种更优雅的方式来做到这一点。
-
你为什么不在下面马丁的回答中使用选择器?
-
好的,我明白你的意思了。您可以使用
tag[attribute]获取属性的文本。例如:content = html.select_one("meta[name='description']")['content']都在一行中。
标签: python beautifulsoup