如何使用Beautiful Soup查找具有自定义html属性的所有元素,而不管html标签如何? [英] How to find all elements with a custom html attribute regardless of html tag using Beautiful Soup?

查看:229
本文介绍了如何使用Beautiful Soup查找具有自定义html属性的所有元素,而不管html标签如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在两种情况下,我想使用自定义html属性来抓取html标签 这是html的示例.如何抓取具有自定义属性"limit"的所有元素.

I have two cases where i want to scrape html tags with custom html attributes This is the example of the html. How do you scrape all the elements with the custom attribute "limit".

<div class="names" limit="10">Bar</div> 
<div id="30" limit="20">Foo</div> 
<li limit="x">Baz</li>

第二种情况相似,但所有html标记都相同

The second case is similar but with all the same html tags

<div class="names" limit="10">Bar</div> 
<div class="names" limit="20">Bar</div> 
<div class="names" limit="30">Bar</div> 

我的问题不同于如何查找仅包含标签的标签某些属性-BeautifulSoup ,因为后者针对具有特定标签的属性值,而我的问题仅与标签或值无关地查找属性

My question is different than How to find tags with only certain attributes - BeautifulSoup because the latter targets attribute values with a specific tag whereas my question finds attributes only regardless of tag or value

推荐答案

# First case:
soup.find_all(attrs={"limit":True})

# Second case:
soup.find_all("div", attrs={"limit":True})

参考:

  • http://www.crummy.com/software/BeautifulSoup/bs4/doc/#kwargs
  • http://www.crummy.com/software/BeautifulSoup/bs4/doc/#find-all

如果您的属性名称不与Python关键字或名为args的soup.find_all冲突,则语法更简单:

If your attribute name doesn't collide with either Python keywords or soup.find_all named args, the syntax is simpler:

soup.find_all(id=True)

这篇关于如何使用Beautiful Soup查找具有自定义html属性的所有元素,而不管html标签如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆