【发布时间】:2016-04-25 19:06:44
【问题描述】:
我想知道如何使用漂亮的汤/请求从一个网站抓取多个不同的页面,而不必一遍又一遍地重复我的代码。
下面是我当前的代码,它正在爬取某些城市的旅游景点:
RegionIDArray = [187147,187323,186338]
dict = {187147: 'Paris', 187323: 'Berlin', 186338: 'London'}
already_printed = set()
for reg in RegionIDArray:
for page in range(1,700,30):
r = requests.get("https://www.tripadvisor.de/Attractions-c47-g" + str(reg) + "-oa" + str(page) + ".html")
g_data = soup.find_all("div", {"class": "element_wrap"})
for item in g_data:
header = item.find_all("div", {"class": "property_title"})
item = (header[0].text.strip())
if item not in already_printed:
already_printed.add(item)
print("POI: " + str(item) + " | " + "Location: " + str(dict[reg]) + " | " + "Art: Museum ")
到目前为止,一切都按预期工作。下一步,除了旅游景点,我想爬取这些城市最受欢迎的博物馆。
因此,我必须通过更改 c 参数来修改请求,以获取所有必需的博物馆:
r = requests.get("https://www.tripadvisor.de/Attractions-c" + str(museumIDArray) +"-g" + str(reg) + "-oa" + str(page) + ".html")
因此我的代码如下所示:
RegionIDArray = [187147,187323,186338]
museumIDArray = [47,49]
dict = {187147: 'Paris', 187323: 'Berlin', 186338: 'London'}
already_printed = set()
for reg in RegionIDArray:
for page in range(1,700,30):
r = requests.get("https://www.tripadvisor.de/Attractions-c" + str(museumIDArray) +"-g" + str(reg) + "-oa" + str(page) + ".html")
soup = BeautifulSoup(r.content)
g_data = soup.find_all("div", {"class": "element_wrap"})
for item in g_data:
header = item.find_all("div", {"class": "property_title"})
item = (header[0].text.strip())
if item not in already_printed:
already_printed.add(item)
print("POI: " + str(item) + " | " + "Location: " + str(dict[reg]) + " | " + "Art: Museum ")
这似乎并不完全正确。我得到的输出,不包括某些城市的所有博物馆和旅游景点。
谁能帮我解决这个问题?感谢您提供任何反馈。
【问题讨论】:
-
你的代码会出错,还有什么是 dict 在你的代码栏中隐藏了一个 python 内置函数?
-
@PadraicCunningham "shadowing a python builtin" 是什么意思对不起,如果我让你紧张,但我还是个初学者
-
dict 是一个 python 类型/函数,最好避免隐藏,即对变量使用与内置类型相同的名称。你能添加一个链接并准确解释你想从中解析什么吗?
-
@PadraicCunningham 这是链接:tripadvisor.de/… 从这个链接我想解析各个项目的标题,比如奥赛博物馆或卢浮宫
标签: python-3.x request beautifulsoup web-crawler