[英]Python Beautiful Soup Extracting HTML Meta Data
我得到了一些我不太了解的奇怪行为。 我希望有人能解释发生了什么。
考虑以下元数据:
<meta property="og:title" content="This is the Tesla Semi truck">
<meta name="twitter:title" content="This is the Tesla Semi truck">
此行成功找到所有“ og”属性并返回列表。
opengraphs = doc.html.head.findAll(property=re.compile(r'^og'))
但是,此行无法对twitter卡执行相同的操作。
twitterCards = doc.html.head.findAll(name=re.compile(r'^twitter'))
为什么第一行成功找到所有“ og”(Opengraph卡),但找不到推特卡?
这是因为name
是标签名称参数的名称,这基本上意味着在这种情况下, BeautifulSoup
将查找标签名称以twitter
开头的元素。
为了指定您实际上是指属性,请使用:
doc.html.head.find_all(attrs={'name': re.compile(r'^twitter')})
或者,通过CSS选择器 :
doc.html.head.select("[name^=twitter]")
其中^=
表示“开头为”。
问题是name=
,它具有特殊含义。 它用于查找标签名称-在您的代码中是meta
您必须添加"meta"
并使用带有"name"
字典
带有不同项目的示例。
from bs4 import BeautifulSoup
import re
data='''
<meta property="og:title" content="This is the Tesla Semi truck">
<meta property="twitter:title" content="This is the Tesla Semi truck">
<meta name="twitter:title" content="This is the Tesla Semi truck">
'''
head = BeautifulSoup(data)
print(head.findAll(property=re.compile(r'^og'))) # OK
print(head.findAll(property=re.compile(r'^tw'))) # OK
print(head.findAll(name=re.compile(r'^meta'))) # OK
print(head.findAll(name=re.compile(r'^tw'))) # empty
print(head.findAll('meta', {'name': re.compile(r'^tw')})) # OK
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.