在python中用BeautifulSoup选择几个标签项
Posted
技术标签:
【中文标题】在python中用BeautifulSoup选择几个标签项【英文标题】:Select several tag items with BeautifulSoup in python 【发布时间】:2019-04-26 15:50:43 【问题描述】:我有下一个html:
<html>
<body>
...
</article>
<article class="issue">
<div class="issue-nr">#39</div>
<div class="issue-date">
<time datetime="2018-04-29T07:30:02+01:00">Apr 29, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #39</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
<article class="issue">
<div class="issue-nr">#38</div>
<div class="issue-date">
<time datetime="2018-04-28T07:30:00+01:00">Apr 28, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #38</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
<article class="issue">
<div class="issue-nr">#37</div>
<div class="issue-date">
<time datetime="2018-04-27T07:30:02+01:00">Apr 27, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #37</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
...
</body>
</html>
我想遍历每篇文章的标签,我真的很喜欢:
from requests import get
from bs4 import BeautifulSoup
response = get("https://example.com")
soup = BeautifulSoup(response.text, "html.parser")
issues = soup.find_all("article", "class": "issue")
for issue in issues:
print (issue)
现在我想从每个文章标签中选择带有“描述”类的跨度标签,但是当我调用“issue.span”时,只选择找到的第一个标签。
有什么建议吗?
提前致谢。
【问题讨论】:
【参考方案1】:你的意思如下。组合使用 CSS 选择器?我使用descendant combinator 来组合选择器,这样您就可以得到article.issue
的span.description
子级。这种编写方式意味着您只会获得描述存在的地方,因此不需要额外的测试。
from bs4 import BeautifulSoup
html = '''
<html>
<body>
...
</article>
<article class="issue">
<div class="issue-nr">#39</div>
<div class="issue-date">
<time datetime="2018-04-29T07:30:02+01:00">Apr 29, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #39</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
<article class="issue">
<div class="issue-nr">#38</div>
<div class="issue-date">
<time datetime="2018-04-28T07:30:00+01:00">Apr 28, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #38</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
<article class="issue">
<div class="issue-nr">#37</div>
<div class="issue-date">
<time datetime="2018-04-27T07:30:02+01:00">Apr 27, 2018</time>
</div>
<div class="issue-title">
<h1>
<a href="/" rel="" target="" title="Title"><span class="subject">The... - #37</span>
<span class="description">
–
Blah, Bleh, Blih ...
</span>
</a></h1>
</div>
</article>
...
</body>
</html>
'''
soup = BeautifulSoup(html, "lxml")
descriptions = soup.select('article.issue span.description')
descriptions = [description.text for description in descriptions]
print(descriptions)
结果:
对于您,您需要从issue
中选择span.description
print([issue.select('span.description') for issue in issues])
【讨论】:
+1 用于使用 css。我有时认为漂亮的汤是阻止python用户学习css的阴谋。 @pguardiario 谢谢。我是 python 新手,但无法想象不使用 css。 这可能是因为你是一个合法的编码人员,而不是那些大多只是从 SO 中粘贴内容的临时用户 :)以上是关于在python中用BeautifulSoup选择几个标签项的主要内容,如果未能解决你的问题,请参考以下文章
Python:告诉BeautifulSoup从两个中选择一个值
利用Python网络爬虫采集天气网的实时信息—BeautifulSoup选择器