繁体   English   中英

从下拉菜单中抓取价值

[英]scrape the value from a dropdown

我正在尝试使用Python与Selenium和Beautiful Soup的组合从Web上的下拉列表元素中抓取值和文本。

我可以获取文本,但无法通过get_attribute命令获取值。

当我打印位于网页上的元素时,它将返回以下内容

打印(价格)

得到它的打印语句给出错误:

None Type object is not callable

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

print(price)的输出是

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

该网页的网址是

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

尝试这个:

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()

它将打印

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

希望这就是你想要的

这是因为get_attribute似乎为None 这不是prices对象的有效属性。 因此,这不是您可以调用的函数-因此会出现错误。 如果您prices.get_attribute括号并仅打印prices.get_attribute不会打印任何内容,因为该值为None

另外, <select>标记首先没有“值”属性。 您要做的是获取了<select>标记,它都是子标记。 <select>标记( <option>标记)中的每个子代都有一个“值”属性。 如果尝试获取该<select>中所有<option>标记的所有值,则应执行以下操作:

price=soup.find("select",{"id":"space-prices"})

# get all <options> in a list
options = price.find_all("option")

# for each element in that list, pull out the "value" attribute
values = [o.get("value") for o in options]
print(values)
#[u'\xa3360', u'\xa31,260', u'\xa35,460', u'\xa316,380', u'\xa365,520']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM