gpt4 book ai didi

python - 使用python从网站爬取多个页面

转载 作者:太空宇宙 更新时间:2023-11-03 15:22:15 25 4
gpt4 key购买 nike

我想知道如何使用 beautiful soup 从一个网站抓取一个城市(例如伦敦)的多个不同页面,而不必一遍又一遍地重复我的代码。

我的目标是理想情况下首先抓取与一个城市相关的所有页面

下面是我的代码:

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=Paris& customerSearch=1&page=0"
response = session.get(jsonUrl, headers=headers)
js_dict = (json.loads(response.content.decode('utf-8')))

for item in js_dict:
headers = js_dict['searchResults']["tours"]
prices = js_dict['searchResults']["tours"]

for title, price in zip(headers, prices):
title_final = title.get("title")
price_final = price.get("price")["original"]

print("Header: " + title_final + " | " + "Price: " + price_final)

输出如下:

Header: London Travelcard: 1 Tag lang unbegrenzt reisen | Price: 19,44 €
Header: 105 Minuten London bei Nacht im verdecklosen Bus | Price: 21,21 €
Header: Ivory House London: 4 Stunden mittelalterliches Bankett| Price: 58,92 €
Header: London: Themse Dinner Cruise | Price: 96,62 €

它只给我返回第一页的结果(4 个结果),但我想获得伦敦的所有结果(必须超过 200 个结果)

你能给我一些建议吗?我想,我必须计算 jsonURL 上的页面,但不知道该怎么做

更新

感谢您的帮助,我能够更进一步。

在本例中,我只能抓取一页 (page=0),但我想抓取前 10 页。因此,我的方法如下:

相关代码片段:

soup = bs4.BeautifulSoup(response.content, "html.parser")
metaConfig = soup.find("meta", property="configuration")

page = 0
while page <= 11:
page += 1

jsonUrl = "https://www.citydis.com/s/results.json?&q=Paris& customerSearch=1&page=" + str(page)
response = session.get(jsonUrl, headers=headers)
js_dict = (json.loads(response.content.decode('utf-8')))


for item in js_dict:
headers = js_dict['searchResults']["tours"]
prices = js_dict['searchResults']["tours"]

for title, price in zip(headers, prices):
title_final = title.get("title")
price_final = price.get("price")["original"]

print("Header: " + title_final + " | " + "Price: " + price_final)

我正在获取特定页面的结果,但不是全部。除此之外,我还收到一条错误消息。这是否与我无法取回所有结果有关?

输出:

Traceback (most recent call last):
File "C:/Users/Scripts/new.py", line 19, in <module>
AttributeError: 'list' object has no attribute 'update'

感谢您的帮助

最佳答案

您确实应该确保您的代码示例完整(您缺少导入)并且语法正确(您的代码包含缩进问题)。在尝试制作一个可行的示例时,我想出了以下内容。

import requests, json, bs4
session = requests.Session()
session.cookies.get_dict()
url = 'http://www.getyourguide.de'
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 = bs4.BeautifulSoup(response.content, "html.parser")
metaConfig = soup.find("meta", property="configuration")
metaConfigTxt = metaConfig["content"]
csrf = json.loads(metaConfigTxt)["pageToken"]


jsonUrl = "https://www.getyourguide.de/s/results.json?&q=London& customerSearch=1&page=0"
headers.update({'X-Csrf-Token': csrf})
response = session.get(jsonUrl, headers=headers)
js_dict = (json.loads(response.content.decode('utf-8')))
print(js_dict.keys())

for item in js_dict:
headers = js_dict['searchResults']["tours"]
prices = js_dict['searchResults']["tours"]

for title, price in zip(headers, prices):
title_final = title.get("title")
price_final = price.get("price")["original"]

print("Header: " + title_final + " | " + "Price: " + price_final)

这给了我超过四个结果。

一般来说,您会发现许多返回 JSON 的站点会对它们的回复进行分页,每页提供固定数量的结果。在这种情况下,每个页面(最后一页)通常都会包含一个键,该键的值会为您提供下一页的 URL。循环页面并在检测到缺少该键时break跳出循环是一件简单的事情。

关于python - 使用python从网站爬取多个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43440410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com