简体   繁体   English

使用python获取下拉HTML框中的数据

[英]Obtain data in drop down html box using python

I want to get the models in the drop box: 我想在下拉框中获取模型:

http://www.trademe.co.nz/motors/used-cars/bmw http://www.trademe.co.nz/motors/used-cars/bmw

from bs4 import BeautifulSoup
import requests 
url="http://www.trademe.co.nz/motors/used-cars/bmw"
r= requests.get(url)
soup = BeautifulSoup(r.content)


price=soup.find("select",{"id":"15"})
options = price.find_all("option")


print(options)

Output 产量

[<option selected="selected" value="">Any model</option>]

I want a list of all the models in the dropbox. 我想要下拉列表中所有模型的列表。

Looks like the data is loaded using ajax. 看起来数据是使用ajax加载的。 This way when you request the page with python, there is still only one option loaded. 这样,当您使用python请求页面时,仍然只加载一个选项。

Checking the Network tab in the chrome debugger you can get this url which returns the models for BMW . 检查chrome调试器中的“ 网络”选项卡 ,您可以获得此URL ,该URL返回BMW的模型。 Now you can requests this url. 现在,您可以请求该网址。

import requests
url = "http://www.trademe.co.nz/API/Ajax/UsedCarsModels.aspx?make=BMW"
data = requests.get(url).json()
print data['models']

Hope this helps you 希望这对您有帮助

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

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