python - 试图抓取 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)
解决方案
您编写它的方式,只有house_dict
当您在块中遇到异常时才会打印字典try
(这无论如何都不起作用 - 在try
块内遇到异常意味着您尝试放置的变量之一insidehouse_dict
不会被定义,这将NameError
在except
块中引发 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)
推荐阅读
- sqlite - 获取列中的最新日期
- react-native - TypeError:未定义不是对象(评估'o.NetInfo.isConnected')
- javascript - 为什么我在 page.on 函数上的返回没有按预期工作
- email - 编辑特定列中的单元格时的 Google 表格通知
- compiler-errors - 编译错误与运行时错误
- java - api 响应不可读
- image - 如何在 Flutter 中将图像转换为文件?
- java - 我的 JTextArea 不尊重组件的大小并被切断
- oracle - 在 Oracle DB 中加载时,CSV 文件和控制文件中的列名不同
- javascript - 如何在 reactjs 和 createElement 中使用窗口、文档事件,如 appendChild