python-3.x Beautifulsoup正在创建一个空的csv文件

vs3odd8k  于 2023-02-20  发布在  Python
关注(0)|答案(1)|浏览(175)
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是空的,我找不到一种方法来使它工作。

wljmcqd8

wljmcqd81#

  • 总是,首先,看看你的汤,看看是否所有预期的成分到位。*

有三个主要问题会导致导致空CSV的行为:

  • user-agent添加到您的请求标头,以获得预期的源:
requests.get(url,headers={'user-agent':'some agent'})
  • 检查您的选择并以适当的语法提供属性:
hotels = soup.find_all('div', {'data-testid':"property-card"})
  • 在应用方法之前,检查元素是否存在。

也可以选中处理替代选择的existing answers for scraping booking.com

相关问题