import requests
from bs4 import BeautifulSoup
import csv
# Set the URL to scrape
url = 'https://www.booking.com/searchresults.en-gb.html?ss=Hurghada&sb=1'
# Send a request to the URL and get the page content
response = requests.get(url)
content = response.content
# Parse the HTML content using Beautiful Soup
soup = BeautifulSoup (content)
print(soup)
# Find the hotel elements
hotels = soup.find_all('div', {'data-testid="property-card"'})[:10]
print(hotels)
# Create a CSV file to save the hotel data
csv_file = open ( 'booking.csv' , 'w' , newline='' , encoding='utf-8' )
writer = csv.writer ( csv_file )
# Write the header row to the CSV file
writer.writerow ( [ 'Hotel Name' , 'Score' , 'Review Count' , 'Price' ] )
# Loop through the first 10 hotels and extract the data
for hotel in hotels [ :10 ]:
# Extract the hotel name
name = hotel.find ( 'div' , class_='sr-hotel__name' ).text.strip ( )
# Extract the hotel score
score = hotel.find ( 'div' , class_='bui-review-score__badge' ).text.strip ( )
# Extract the review count
review_count = hotel.find ( 'div' , class_='bui-review-score__text' ).text.strip ( )
# Extract the price
price = hotel.find ( 'div' , class_='sr_rooms_table_block__prices' ).text.strip ( )
# Write the hotel data to the CSV file
writer.writerow ( [ name , score , review_count , price ] )
# Close the CSV file
csv_file.close ( )
我正在尝试制作一个csv文件,其中有以下的前10个结果:
1-酒店名称
2-评定分数,例如8.7或8
3-评级值,例如优秀或非常好
4-审查次数
csv是空的,我找不到一种方法来使它工作。
1条答案
按热度按时间wljmcqd81#
有三个主要问题会导致导致空CSV的行为:
user-agent
添加到您的请求标头,以获得预期的源:也可以选中处理替代选择的existing answers for scraping booking.com。