【问题标题】:Beautifulsoup finding a specific value in meta tagsBeautifulsoup 在元标记中找到特定值
【发布时间】:2017-11-15 14:11:21
【问题描述】:

我正在尝试查找其中包含作者的所有元标记。如果我有一个特定的键和正则表达式值,它就可以工作。当两者都是正则表达式时它不起作用。是否可以提取页面上包含“作者”关键字的所有元标记? 这是我写的代码。

from bs4 import BeautifulSoup
page = requests.get(url)
contents = page.content
soup = BeautifulSoup(contents, 'lxml')
preys = soup.find_all("meta", attrs={re.compile('.*'): re.compile('author')})

编辑: 为了澄清起见,我要专门解决的问题是值“作者”是否映射到任何键。正如我在各种示例中看到的那样,该键可以是“itemprop”、“name”甚至“property”。基本上,我的问题是提取所有将作者作为值的元标记,无论该值具有什么键。 几个例子:

<meta content="Jami Miscik" name="citation_author"/>
<meta content="Will Ripley, Joshua Berlinger and Allison Brennan, CNN" itemprop="author"/>
<meta content="Alison Griswold" property="author"/>

【问题讨论】:

  • 文档是否在某处建议属性名称可以是正则表达式?我在crummy.com/software/BeautifulSoup/bs4/doc/#attrs 下找不到任何提示
  • 可能是这样。如果是这样,我将不得不收集所有潜在的键并检查它们的值。

标签: python regex beautifulsoup


【解决方案1】:

应该这样做。很遗憾,我无法快速找到包含 author meta 的页面来证明此代码的有效性。如果您发现有问题,请告诉我。

>>> import requests
>>> import bs4
>>> page = requests.get('http://reference.sitepoint.com/html/meta').text
>>> soup = bs4.BeautifulSoup(page, 'lxml')
>>> [item.attrs['name'] for item in soup('meta') if item.has_attr('name')]
['robots', 'description']
>>> [item.attrs['name'] for item in soup('meta') if item.has_attr('name') and item.attrs['name'].lower()=='author']
[]

编辑:也适用于 Jan 的 html 块。他的语法更好;使用它。

>>> html = '<meta name="author" content="Anna Lyse"> <meta name="date" content="2010-05-15T08:49:37+02:00">'
>>> soup = bs4.BeautifulSoup(html, 'lxml')
>>> [item.attrs['name'] for item in soup('meta') if item.has_attr('name') and item.attrs['name'].lower()=='author']
['author']

【讨论】:

  • 只需构造一个示例字符串 :)
  • ...为什么我没有想到呢?
  • 你是心理医生。
  • :) 我想忘记BeautifulSoup 的一些语法并不是精神障碍的标准——至少我不知道。
  • @Jan:我在编辑我的答案时使用了您的 HTML。也谢谢你。
【解决方案2】:

如果您正在寻找citation_authorauthor,您可能会使用soup.select() 和正则表达式的组合:

from bs4 import BeautifulSoup
import re

# some test string
html = '''
<meta name="author" content="Anna Lyse">
<meta name="date" content="2010-05-15T08:49:37+02:00">
<meta itemprop="author" content="2010-05-15T08:49:37+02:00">
<meta rel="author" content="2010-05-15T08:49:37+02:00">
<meta content="Jami Miscik" name="citation_author"/>
<meta content="Will Ripley, Joshua Berlinger and Allison Brennan, CNN" itemprop="author"/>
<meta content="Alison Griswold" property="author"/>
'''

soup = BeautifulSoup(html, 'html5lib')

rx = re.compile(r'(?<=)"(?:citation_)?author"')

authors = [author 
            for author in soup.select("meta")
            if rx.search(str(author))]

print(authors)

【讨论】:

  • 我试图记住的语法——但在文档中找不到。
  • @BillBell:它有点​​隐藏,可以在crummy.com/software/BeautifulSoup/bs4/doc/#css-selectors 下找到 - 测试属性是否存在:
  • 谢谢。我一直说我会尝试以不同的形式编写 BS 文档——为像我这样的人。
  • 非常感谢!我要专门解决的问题是“作者”值是否映射到任何键。正如我在各种示例中看到的那样,该键可以是 itemprop、名称甚至是属性。此解决方案假定键是“名称”,但情况可能并非如此。
  • 效果很好!非常感谢。
猜你喜欢
  • 2019-01-16
  • 2012-01-03
  • 1970-01-01
  • 2020-10-05
  • 2018-10-11
  • 1970-01-01
  • 2018-07-06
  • 2013-12-17
  • 1970-01-01
相关资源
最近更新 更多