【发布时间】:2017-09-12 03:58:27
【问题描述】:
我目前面临的问题是我无法从特定网站上抓取我想要的信息。
详细来说,我想获取 JSON 中的所有观光项目和价格。
到目前为止,我能够收回所有价格,但无法收回所有商品。我只是拿回一件特定的物品。
不确定是什么问题。
到目前为止,这是我的逻辑:
session = requests.Session()
session.cookies.get_dict()
url = 'http://www.citydis.com'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
response = session.get(url, headers=headers)
soup = BeautifulSoup(response.content, "html.parser")
metaConfig = soup.find("meta", property="configuration")
jsonUrl = "https://www.citydis.com/s/results.json?&q=London& customerSearch=1&page=0"
js_dict = (json.loads(response.content.decode('utf-8')))
for item in js_dict:
header = (js_dict['searchResults']["tours"])
for titles in header:
title_final = (titles.get("title"))
url = (js_dict['searchResults']["tours"])
for urls in url:
url_final = (urls.get("url"))
price = (js_dict['searchResults']["tours"])
for prices in price:
price_final = (prices.get("price")["original"])
print("Header: " + title_final + " | " + "Price: " + price_final)
这是输出:
Header: Ticket für Madame Tussauds London & Star-Wars-Erlebnis | Price: 83,66 €
Header: Ticket für Madame Tussauds London & Star-Wars-Erlebnis | Price: 37,71 €
Header: Ticket für Madame Tussauds London & Star-Wars-Erlebnis | Price: 152,01 €
正如你们所看到的,价格显示正确,但项目(标题)没有不同。我只是拿回一件特定的物品。
你们能帮帮我吗?任何反馈表示赞赏。
【问题讨论】:
标签: python json beautifulsoup request web-crawler