首页 > 解决方案 > 试图抓取 Airbnb 数据

问题描述

所以我试图从 Airbnb 中抓取一些数据(名称、价格、评级),我可以打印出价格、名称和评级等变量,但我想将它们放入字典中。我错过了什么?

import requests
from bs4 import BeautifulSoup

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'
}

url = 'https://www.airbnb.com/s/Tbilisi--Georgia/homes?tab_id=home_tab&refinement_paths%5B%5D=%2Fhomes&flexible_trip_dates%5B%5D=november&flexible_trip_dates%5B%5D=october&flexible_trip_lengths%5B%5D=weekend_trip&date_picker_type=calendar&query=Tbilisi%2C%20Georgia&place_id=ChIJa2JP5tcMREARo25X4u2E0GE&source=structured_search_input_header&search_type=autocomplete_click'

response = requests.get(url, headers=headers)

soup = BeautifulSoup(response.content, 'lxml')



for item in soup.find_all('div', itemprop='itemListElement'):

    try:
        price = item.find('span', class_='_krjbj').text
        rating = item.find('span', class_='_18khxk1').text
        name = item.find('meta', itemprop='name')['content']
    except Exception as e:
        house_list = {
            'price': price,
            'rating': rating,
            'name': name,
        }
        print(house_list)

标签: pythonbeautifulsoup

解决方案


您编写它的方式,只有house_dict当您在块中遇到异常时才会打印字典try(这无论如何都不起作用 - 在try块内遇到异常意味着您尝试放置的变量之一insidehouse_dict不会被定义,这将NameErrorexcept块中引发 a )。

你可能想做这样的事情:

# ...
    try:
        price = item.find('span', class_='_krjbj').text
        rating = item.find('span', class_='_18khxk1').text
        name = item.find('meta', itemprop='name')['content']
    except Exception as e:
        print("Ran into an Exception when trying to parse data")
        continue
    else:
        house_list = {
            'price': price,
            'rating': rating,
            'name': name,
        }
        print(house_list)

推荐阅读