Rojo Rojo - 19 days ago 13
Python Question

scrape the value from a dropdown

I am trying to web scrape the value and text from a dropdown element on a webpage using a combination of Python with selenium and Beautiful Soup.

I am able to get the text but I am not able to get the value through the

get_attribute
command.

When I print the element that I located on the webpage it returns the following content

print(price)

The print statement that gets it gives the error:

None Type object is not callable

price=soup.find("select",{"id":"space-prices"})
print(price)
print(price.text)
print(price.get_attribute('value'))


The output for print(price) is

<select class="pricing-bar-select" id="space-prices" name="space-prices"><option selected="selected" value="£360">Per Day</option>
<option value="£1,260">Per Week</option>
<option value="£5,460">Per Month</option>
<option value="£16,380">Per Quarter</option>
<option value="£65,520">Per Year</option></select>


The URL of the webpage is

https://www.appearhere.co.uk/spaces/north-kensington-upcycling-store-and-cafe

Answer

try this:

from selenium import webdriver
from bs4 import BeautifulSoup


driver = webdriver.Chrome()
url= "https://www.appearhere.co.uk/spaces/north-kensington-upcycling-store-and-cafe"
driver.maximize_window()
driver.get(url)

content = driver.page_source.encode('utf-8').strip()
soup = BeautifulSoup(content,"html.parser")
price=soup.find("select",{"id":"space-prices"})
options = price.find_all("option")
options1=[y.text for y in options]
values = [o.get("value") for o in options]
for x in range(5):
    print options1[x], values[x].encode('utf8')
driver.quit()

It will print

Per Day £360
Per Week £1,260
Per Month £5,460
Per Quarter £16,380
Per Year £65,520

Hope this is what you want

Comments