Nitin
Nitin

Reputation: 147

Fetching correct node using css selector in Beautiful Soup?

For the link,

http://www.jabong.com/Adidas-Base-Mid-Dd-Blue-Round-Neck-T-Shirt-2733238.html

...I need to get the product fabric detail, "Polyster". But I get "Fabric" as output. Below is the part of code.

soup.find_all("span", {"class":"product-info-left"})[0].text

Upvotes: 1

Views: 49

Answers (3)

宏杰李
宏杰李

Reputation: 12158

The info you need is in the li tag which is under the ul tag, you should find the ul first, then you can get all the text in the li tag using stripped_strings

In [47]: r = requests.get('http://www.jabong.com/Adidas-Base-Mid-Dd-Blue-Round-Neck-T-Shirt-2733238.html')

In [48]: soup = BeautifulSoup(r.text, 'lxml')

In [49]: ul = soup.find('ul', class_="prod-main-wrapper")

In [50]: for li in ul.find_all('li'):
    ...:     print(list(li.stripped_strings))
    ...:     
['Fabric', 'Polyester']
['Sleeves', 'Half Sleeves']
['Neck', 'Round Neck']
['Fit', 'Regular']
['Color', 'Blue']
['Style', 'Solid']
['SKU', 'AD004MA61NGOINDFAS']
['Model Stats', 'This model has height 6\'0",Chest 38",Waist 34"and is Wearing Size M.']
['Authorization', 'Adidas authorized online sales partner.', 'View Certificate']

If you only want the first row, you can use find(), it will return the fists element in the find_all():

In [51]: text = ul.find('li').stripped_strings  
In [52]: print(list(text))
['Fabric', 'Polyester']

Upvotes: 0

akash karothiya
akash karothiya

Reputation: 5950

You can use .next or .next_sibling here :

>>> soup.find_all("span", {"class":"product-info-left"})[0].next.next.text
'Polyester'
>>> soup.find_all("span", {"class":"product-info-left"})[0].next_sibling.text
'Polyester'

Upvotes: 0

Zroq
Zroq

Reputation: 8392

Find you node next_sibling.

soup.find_all("span", {"class":"product-info-left"})[0].next_sibling.text

Upvotes: 1

Related Questions