如何使用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
- 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)