Get an Attribute Value Based on the Name Attribute with Beautifulsoup

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']
'Austin'

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']

Python: BeautifulSoup - Get an attribute value from the name of a class

You are not iterating through the links list. Try this.

links = soup.find_all("a", class_="iusc")

for link in links:
print(link.get('m'))

how to get class attribute by beautifulSoup?

At first you need to identify span tag using find() then need to find_all() all i tag then get attribute class which returns as list and then get the last index value.

from bs4 import BeautifulSoup

html='''<span class="attribute-value">
<i class="icon icon_male_symbol"></i>
<i class="icon icon_female_symbol"></i>
</span>'''

soup=BeautifulSoup(html,'html.parser')
for item in soup.find('span',class_='attribute-value').find_all('i'):
print(item['class'][-1])

Beautifulsoup: Is it possible to get tag name and attribute name by its value?

You could define a filter function that checks if there is one HTML tag with a attribute value equal to value:

def your_filter(tag, value):
for key in tag.attrs.keys():
if tag[key] == value:
return True
return False

# alternatively as one liner:
def your_filter(tag, value):
return any(tag[key] == value for key in tag.attrs.keys())

Then, you could use it like this:


soup = BeautifulSoup(html_code)
tags = soup.find_all(lambda tag: your_filter(tag, "icaec13e17ee4432d9971f5e4b3d32ba1_265"))

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.

How can I get a value from an attribute inside a tag as a int

What happens?

You are extracting a string with .text but to use the / operand it should be an int

How to fix?

First at all, clean your string from non digit characters:

...find(class_='price').text.split('₽')[0].replace(' ','')

For calculating convert it with int() to an integer:

int(price_2)/x 

Example

Note Changed the find() for these example, cause your question do not provide an correct html

from bs4 import BeautifulSoup

html = '''
<p class="product-discount__price js-product-discount-price">
<span class="price">3 033 <span class="currency w500">₽<span class="currency_seo">руб.</span></span></span>
</p>'''

soup = BeautifulSoup(html, 'lxml')
price_2 = soup.find(class_='product-discount__price').find(class_='price').text.split('₽')[0].replace(' ','')
x = 2
price_3 = int(price_2)/x
print(price_3)

Output

1516.5

How to get attribute values of HTML tags into a list?

Few issues. First, tag is list of elements, specifically all the <tr> tag elements. Secondly, not all the <tr> tags have an 'id' attribute.

So you need to put in some logic for that:

import requests
from bs4 import BeautifulSoup

url = "https://brawlify.com/stats/club/V8GVVR0R"
result = requests.get(url).text
doc = BeautifulSoup(result, "html.parser")

tag = doc.find_all('tr')

attribute = [x['id'] for x in tag if 'id' in x.attrs]

Output:

print(attribute)
['8LVPCRJGR', '29G9VJJC', '2YP08GUG8', 'UY8PVUPL', 'VV2RRRGG', '20RQQ08U9', 'VJ00J8Y8', '200PG2VLP', '28QV0RJVV', 'YRLPJ80J', 'PRLV99U89', '9QJLQGGU', '88UYYG0U', '9PG8RUVJ', 'YP9UQ8CQ', '9J8LRGQU2', '2LPGYQVV9', '8C8CJ0UJU', 'GUGJLLRG', '9Q0VCV2J', '2RVYVL8YL', 'JP0VGC2P', '280GY2R2C', '2PRLQPJJY', '8CGJGPYJ9', '89RYCVQJ0', '80GVU28CC', 'UV0CPU2Q', '9RGG9J08J', 'Y2PQ8090R']


Related Topics



Leave a reply



Submit