【发布时间】:2019-03-31 07:53:13
【问题描述】:
我似乎无法为第 1 页以外的任何内容生成输出(一页有 15 家餐厅,这就是我得到的全部内容(只有 15 个输出)。看起来第一页的输出被第二页替换,依此类推.
我尝试将页面范围添加到 scrape,但仍然只返回 15 个结果(scraping 只有一页)。
import requests
import pandas
from bs4 import BeautifulSoup
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'}
for num in range(1,5):
url = 'https://www.zomato.com/auckland/restaurants?gold_partner=1&page={}'.format(num)
response = requests.get(url,headers=headers)
content = response.content
soup = BeautifulSoup(content,"html.parser")
top_rest = soup.find_all("div",attrs={"class": "col-s-16 search_results mbot"})
list_tr = top_rest[0].find_all("div",attrs={"class": "js-search-result-li even status 1"})
list_rest =[]
for tr in list_tr:
dataframe ={}
dataframe["1.rest_name"] = (tr.find("a",attrs={"class": "result-title hover_feedback zred bold ln24 fontsize0"})).text.replace('\n', ' ')
dataframe["2.rest_address"] = (tr.find("div",attrs={"class": "col-m-16 search-result-address grey-text nowrap ln22"})).text.replace('\n', ' ')
list_rest.append(dataframe)
list_rest
df = pandas.DataFrame(list_rest)
df.to_csv("zomato_res26.csv",index=False)
我希望得到一份包含 40 多家餐厅及其名称和位置的输出列表,但到目前为止,我似乎每页只有 15 家餐厅
【问题讨论】:
标签: python-3.x pandas web-scraping beautifulsoup python-requests