1

I need to grab the value of the attribute value inside this HTML element

<option value="1129070-040" data-size="US9.5" data-stock="In_Stock" data-morecomingsoon="true">

I can't seem to find a way in bs4 documentation

charlie
  • 389
  • 3
  • 16
donavon
  • 13
  • 2

1 Answers1

1

Based on what you included in your question you can get the value of the value attribute using this code:

from bs4 import BeautifulSoup 

html = """<option value="1129070-040" data-size="US9.5" data-stock="In_Stock" data-morecomingsoon="true">"""    
soup = BeautifulSoup(html,"lxml")    
value = soup.find("option")["value"]

print(value)

hope this helps

Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
  • almost @fozoro ```product_page = requests.get('https://www.net-a-porter.com/de/en/product/1129070') soup = BeautifulSoup (product_page.content, "lxml") product_id = soup.find(here i need to find that value)``` The value is inside this html element ``` – donavon Jun 23 '19 at 16:19
  • what if there's more ("option")["value"] in the webpage, how to find a specific one with exact elements that i gave you like data_size and, data_stock and data_morecomingsoon – donavon Jun 23 '19 at 16:33
  • hmmmm it seems that the website is rendered using Javascript. Which makes it nearly impossible to scrap using `requests` – Nazim Kerimbekov Jun 23 '19 at 16:34
  • not a problem i can use selenium, just wanna know how to find that specific value – donavon Jun 23 '19 at 16:37
  • do you want to get one or all of them? – Nazim Kerimbekov Jun 23 '19 at 16:38
  • let's say just one, with data_size="US7'' and data_stock="InStock" – donavon Jun 23 '19 at 16:39
  • in this case replace my `value` variable with `value = soup.find("option",{"data_size":"US7","data_stock"="InStock"})["value"]` – Nazim Kerimbekov Jun 23 '19 at 16:42
  • Thanks man! do you recommend using node,js/cheerio for a webpage that's rendered in js or stick with py/sb4 and use selenium? I wan parsing to be fast though. – donavon Jun 23 '19 at 16:45
  • never used node.js/cheerio I think selenium is a solid choice in your case though I can't really give any tips on it (I hardly ever use it myself). Thanks for accepting the answer, best of luck! – Nazim Kerimbekov Jun 23 '19 at 16:48