-1

我写了这段代码,但在运行最后一行后得到了错误“IndexError:list index out of range”。请问,我该如何解决这个问题?

    import requests
    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'}
    response = requests.get("https://www.zomato.com/bangalore/top-restaurants",headers=headers)

    content = response.content
    soup = BeautifulSoup(content,"html.parser")

    top_rest = soup.find_all("div",attrs={"class": "sc-bblaLu dOXFUL"})
    list_tr = top_rest[0].find_all("div",attrs={"class": "sc-gTAwTn cKXlHE"})

list_rest =[]
for tr in list_tr:
    dataframe ={}
    dataframe["rest_name"] = (tr.find("div",attrs={"class": "res_title zblack bold nowrap"})).text.replace('\n', ' ')
    dataframe["rest_address"] = (tr.find("div",attrs={"class": "nowrap grey-text fontsize5 ttupper"})).text.replace('\n', ' ')
    dataframe["cuisine_type"] = (tr.find("div",attrs={"class":"nowrap grey-text"})).text.replace('\n', ' ')
    list_rest.append(dataframe)
list_rest
4

3 回答 3

0

我最近做了一个项目,让我研究了在菲律宾马尼拉的 Zomato 网站。我使用 Geolibrary 获取马尼拉市的经度和纬度值,然后使用这些信息抓取餐厅的详细信息。ADD:您可以在zomato网站上获取自己的API密钥,一天最多可以进行1000次调用。

# Use geopy library to get the latitude and longitude values of Manila City.
from geopy.geocoders import Nominatim

address = 'Manila City, Philippines'
geolocator = Nominatim(user_agent = 'Makati_explorer')
location = geolocator.geocode(address)
latitude = location.lenter code hereatitude
longitude = location.longitude
print('The geographical coordinate of Makati City are {}, {}.'.format(latitude, longitude))

# Use Zomato's API to make call
headers = {'user-key': '617e6e315c6ec2ad5234e884957bfa4d'}
venues_information = []

for index, row in foursquare_venues.iterrows():
    print("Fetching data for venue: {}".format(index + 1))
    venue = []
    url = ('https://developers.zomato.com/api/v2.1/search?q={}' + 
          '&start=0&count=1&lat={}&lon={}&sort=real_distance').format(row['name'], row['lat'], row['lng'])
    try:
        result = requests.get(url, headers = headers).json()
    except:
        print("There was an error...")
    try:

        if (len(result['restaurants']) > 0):
            venue.append(result['restaurants'][0]['restaurant']['name'])
            venue.append(result['restaurants'][0]['restaurant']['location']['latitude'])
            venue.append(result['restaurants'][0]['restaurant']['location']['longitude'])
            venue.append(result['restaurants'][0]['restaurant']['average_cost_for_two'])
            venue.append(result['restaurants'][0]['restaurant']['price_range'])
            venue.append(result['restaurants'][0]['restaurant']['user_rating']['aggregate_rating'])
            venue.append(result['restaurants'][0]['restaurant']['location']['address'])
            venues_information.append(venue)
        else:
            venues_information.append(np.zeros(6))
    except:
        pass

ZomatoVenues = pd.DataFrame(venues_information, 
                                  columns = ['venue', 'latitude', 
                                             'longitude', 'price_for_two', 
                                             'price_range', 'rating', 'address'])
于 2020-06-06T10:05:47.167 回答
0

您收到此错误是因为当您尝试获取它的第一个元素时 top_rest 为空"top_rest[0]"。原因是您尝试引用的第一个类是动态命名的。您会注意到,如果您刷新页面,该 div 的相同位置将不会被命名为相同。因此,当您尝试抓取时,您会得到空的结果。

另一种方法是抓取所有 div,然后缩小您想要的元素,注意动态 div 命名模式,以便从一个请求到另一个请求,您将获得不同的结果:

import requests
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'}
response = requests.get("https://www.zomato.com/bangalore/top-restaurants",headers=headers)

content = response.content
soup = BeautifulSoup(content,"html.parser")

top_rest = soup.find_all("div")
list_tr = top_rest[0].find_all("div",attrs={"class": "bke1zw-1 eMsYsc"})
list_tr
于 2020-03-26T01:28:55.310 回答
-1

使用Web Scraping Language我可以这样写:

GOTO https://www.zomato.com/bangalore/top-restaurants
EXTRACT {'rest_name': '//div[@class="res_title zblack bold nowrap"]', 
         'rest_address': '//div[@class="nowrap grey-text fontsize5 ttupper', 
         'cusine_type': '//div[@class="nowrap grey-text"]'} IN //div[@class="bke1zw-1 eMsYsc"]

这将使用类遍历每个记录元素bke1zw-1 eMsYsc并提取每个餐厅信息。

于 2020-04-16T22:01:56.840 回答