Extracting an Attribute Value With Beautifulsoup

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



Leave a reply



Submit