James Bond
James Bond

Reputation: 41

Python get specific data with Beautifulsoup

Hey so i’m trying to get the value of wpid from this website https://www.scan.co.uk/products/1m-canyon-apple-lightning-to-usb-cable-for-apple-non-mfi-certified-iphones-5-6-7-8-x-11-black with python using beautifulsoup but i can’t figure out how to only get the wpid and not the other stuff. help would be appreciated.

page = sess.get(url)
data = page.text
soup = BeautifulSoup(data, 'html.parser')
text = soup.find('div', class_='buyButton large')
text2 = text.find('a')['href']
text3 = (text.find('a').contents[0])
text4 = (soup.find('div', class_='buyButton large').contents[0])
#text3 = text.find('div')['data-wpid']
print(text)
print(text2)
print(text3)
print(text4)

this is the response i get: <div class="buyButton large" data-instock="1" data-source="2" data-wpid="2951361"><a class="btn" href="https://secure.scan.co.uk/web/basket/addproduct/2951361" rel="nofollow">Add To Basket</a></div> https://secure.scan.co.uk/web/basket/addproduct/2951361 Add To Basket <a class="btn" href="https://secure.scan.co.uk/web/basket/addproduct/2951361" rel="nofollow">Add To Basket</a> but i only want the value of the wpid which would be 2951361

Upvotes: 0

Views: 77

Answers (1)

Sven Eberth
Sven Eberth

Reputation: 3116

Access the data-wpid attribute like you already do it, but on the correct element. text is already the div with the attribute, therefore you don't need the extra find().

>>> print("wpid:", text["data-wpid"])
wpid: 2951361

Upvotes: 1

Related Questions