Extracting an attribute value with beautifulsoup
.find_all()
returns list of all found elements, so:
input_tag = soup.find_all(attrs={"name" : "stainfo"})
input_tag
is a list (probably containing only one element). Depending on what you want exactly you either should do:
output = input_tag[0]['value']
or use .find()
method which returns only one (first) found element:
input_tag = soup.find(attrs={"name": "stainfo"})
output = input_tag['value']
Extracting an attribute value from div with beautifulsoup
Looks like the data inside the attribute can be parsed with json
module:
txt = '''<div class="1234" data-component="aaaaa" data-settings='{"baseURL": "https://someurl.com/","importantvalue": "999999999"}'>'''
import json
from bs4 import BeautifulSoup
soup = BeautifulSoup(txt, 'html.parser')
data = json.loads( soup.div['data-settings'] )
print(data['importantvalue'])
Prints:
999999999
How to extract attribute value from a tag in BeautifulSoup
Main issue is that you try to access the attribute key directly, what will return a KeyError
, if the attribute is not available:
currentLine["class"]
Instead use get()
that will return in fact of a missing attribute None
:
currentLine.get("class")
From the docs - get(key\[, default\])
:
Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.
Get an attribute value based on the name attribute with BeautifulSoup
It's pretty simple, use the following -
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('<META NAME="City" content="Austin">')
>>> soup.find("meta", {"name":"City"})
<meta name="City" content="Austin" />
>>> soup.find("meta", {"name":"City"})['content']
u'Austin'
Leave a comment if anything is not clear.
How to access tag's attribute value with BeautifulSoup
You can read the attribute title
value like this:
from bs4 import BeautifulSoup
response = """
<html>
<div class="starRating" title="4.31">
<svg>
</svg>
</div>
</html>
"""
soup = BeautifulSoup(response, 'lxml')
print(soup.find('div', {'class': 'starRating'})['title'])
Outputs:
4.31
See https://www.crummy.com/software/BeautifulSoup/bs4/doc/#attributes
`
A tag may have any number of attributes. The tag
<b id="boldest">
has an attribute “id” whose value is “boldest”. You can access a tag’s attributes by treating the tag like a dictionary
Python Beautifulsoup Getting Attribute Value
You can access the attrs using key-value pair
Ex:
from bs4 import BeautifulSoup
s = """<span class="invisible" data-datenews="2018-05-25 06:02:19" data-idnews="2736625" id="horaCompleta"></span>"""
soup = BeautifulSoup(s, "html.parser")
print(soup.span["data-datenews"])
Output:
2018-05-25 06:02:19
Related Topics
How to Create a New Column from the Output of Pandas Groupby().Sum()
How to Use Glob() to Find Files Recursively
Urllib and "Ssl: Certificate_Verify_Failed" Error
"Large Data" Workflows Using Pandas
How to Find the Duplicates in a List and Create Another List With Them
Wait Until Page Is Loaded With Selenium Webdriver For Python
Flask View Return Error "View Function Did Not Return a Response"
Convert List of Dictionaries to a Pandas Dataframe
Pygame Window Not Responding After a Few Seconds
Tkinter: How to Use After Method
Saving an Object (Data Persistence)
Haversine Formula in Python (Bearing and Distance Between Two Gps Points)