I have the following HTML fragment:
>>> a
<div class="headercolumn">
<h2>
<a class="results" data-name="result-name" href="/xxy> my text</a>
</h2>
I am trying to select header column only if attribute data-name="result-name"
I've tried:
>>> a.select('a["data-name="result-name""]')
This gives:
ValueError: Unsupported or invalid CSS selector:
How can I get this working?
We can access a tag's attributes by treating it like a dictionary. Implementation: Example 1: Program to extract the attributes using attrs approach.
To find elements by attribute in Beautiful Soup, us the select (~) method or the find_all (~) method. Note that the square bracket in CSS denotes attributes.
In this article, we will discuss how beautifulsoup can be employed to find a tag with the given attribute value in an HTML document. Import module. Scrap data from a webpage. Parse the string scraped to HTML. Use find () function to find the attribute and tag. Print the result.
One of the important pieces of element in any piece of HTML document are tags, which may contain other tags/strings (tag’s children). Beautiful Soup provides different ways to navigate and iterate over’s tag’s children. Easiest way to search a parse tree is to search the tag by its name.
To get all attributes of an element, you need to follow this code: Let's see how to get the attribute class. Find all by ul tag. Iterate over the result. Get the class value of each element. In the below example, we'll get the value of the href attribute. attrs={"attribute":"value", "attribute":"value",...}
You can simply do this :
soup = BeautifulSoup(html)
results = soup.findAll("a", {"data-name" : "result-name"})
Source : How to find tags with only certain attributes - BeautifulSoup
html = """
<div class="headercolumn">
<h2>
<a class="results" data-name="result-name" href="/xxy> my text</a>
</h2>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
for d in soup.findAll("div",{"class":"headercolumn"}):
print d.a.get("data-name")
print d.select("a.results")
result-name
[<a class="results" data-name="result-name" href="/xxy> my text</a></h2>"></a>]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With