Я отказался от бронирования на нескольких страницах, используя l oop и веб-драйвер селена. Тем не менее, некоторые из предметов не появляются. Элементы доступны, когда я проверил страницы. Подскажите, пожалуйста, в чем заключается проблема и решение для этого? Я проверил другие сообщения здесь, и все они посоветовали использовать таймер. Я использовал таймер всякий раз, когда он читает новую страницу, но безуспешно.
Я могу получить полную запись, если очищаю одну страницу, но это отнимает много времени. Следовательно, я хотел автоматизировать то же самое. Каждая страница дает 28 записей и смещение второй страницы до 25 по ссылке booking.com.
Здесь я попытался извлечь отели для Веллингтона, и у него есть 4 страницы. Я проверил на две страницы в соответствии с моим кодом. Пожалуйста, помогите и сообщите, что пошло не так?
Мой код ниже
#Importing necessary library
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.firefox.options import Options
from webdriver_manager.chrome import ChromeDriverManager
import pandas as pd
import time
import re
import requests
from PIL import Image
import requests
from io import BytesIO
import matplotlib.pyplot as plt
from bs4 import BeautifulSoup
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from itertools import zip_longest
# Creating an empty list for hotel name, ratings, locations, and description links and appending the list using loop
names = []
rating = []
location = []
links = []
reviews = []
price = []
p1 = []
desc = []
loc = []
src_link = []
category = []
desc = []
driver = webdriver.Chrome(ChromeDriverManager().install())
for pageno in range(0,50,25):
print(pageno)
driver.get("https://www.booking.com/searchresults.en-gb.html?aid=304142&label=gen173nr-1DCAEoggI46AdIM1gEaK4BiAEBmAEJuAEXyAEM2AED6AEBiAIBqAIDuAKhtYn0BcACAQ&sid=560904567b64f1e8c80d883e4882616f&tmpl=searchresults&checkin_month=8&checkin_monthday=1&checkin_year=2020&checkout_month=8&checkout_monthday=4&checkout_year=2020&class_interval=1&dest_id=-1521348&dest_type=city&dtdisc=0&from_sf=1&group_adults=2&group_children=0&inac=0&index_postcard=0&label_click=undef&no_rooms=1&postcard=0&raw_dest_type=city&room1=A%2CA&sb_price_type=total&shw_aparth=1&slp_r_match=0&src=index&src_elem=sb&srpvid=47769c9973ad002d&ss=Wellington&ss_all=0&ssb=empty&sshis=0&ssne=Wellington&ssne_untouched=Wellington&top_ufis=1&rows=25&offset=0" + str(pageno))
time.sleep(5)
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
#Hotel name
for item in soup.findAll('span', {'class': 'sr-hotel__name'}):
names.append(item.get_text(strip=True))
#print(names)
# Number of reviews
for item in soup.findAll('div', {'class' : 'bui-review-score__text'}):
reviews.append(item.get_text(strip = True))
#print(reviews)
#Number of ratings
for item in soup.findAll("div", {'class': 'bui-review-score__badge'}):
rating.append(item.get_text(strip=True))
#print(rating)
# Extracting each hotel links
for item in soup.findAll("a", {'class': 'hotel_name_link url'}):
item = item.get("href").strip("\n")
links.append(f"https://www.booking.com{item}")
#Extracting each hotel image link
for link in soup.find_all("img", class_='hotel_image'):
a = link.attrs["src"]
src_link.append(a)
#Opening each hotel link and extracting location and hotel description
for item in links:
r = requests.get(item)
soup = BeautifulSoup(r.text, 'html.parser')
for item in soup.findAll("div", {'id': 'property_description_content'}):
desc.append(item.get_text("\n", strip=True))
for item in soup.findAll("span", {'class': 'hp_address_subtitle'}):
loc.append(item.get_text(strip = True))
#Extracting hotel category type
for item in links:
driver.get(item)
WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.CSS_SELECTOR,"h2#hp_hotel_name")))
try:
job_title = driver.find_element_by_css_selector("h2#hp_hotel_name>span").text
category.append(job_title)
#print(category)
except:
category.append("None")
# Converting all the details into dataframe and csv file
final = []
for item in zip_longest(names, reviews, rating, desc, loc, src_link, links, category):
final.append(item)
df5 = pd.DataFrame(
final, columns=['Names', 'Reviews','Rating', 'Description', 'Location', 'image', 'links', 'category'])
#df.to_csv('booked.csv')
#driver.quit()
Вывод: Имена отелей, отзывы, рейтинги не отображаются за последние 20 записей.