【发布时间】:2021-10-07 17:30:00
【问题描述】:
我想学习 python,为此我从一个小型网络抓取项目开始。 我想为一家旅行社做一个有竞争力的记分卡,首先这里是网站链接:tn.tunisiebooking.com
如您所见,您必须填写表格,然后将显示酒店列表来自主页的数据。
如果您能帮助我并向我解释为什么会这样,请提前感谢您。这是我使用的代码:
import timer
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from bs4 import BeautifulSoup
import requests
PATH="C:\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get('https://tn.tunisiebooking.com/')
wait = WebDriverWait(driver, 20)
# write script
script = "document.getElementById('ville_des').value ='Sousse';document.getElementById('depart').value ='05/08/2021';document.getElementById('checkin').value ='05/08/2021';document.getElementById('select_ch').value = '1';"
# generate a alert via javascript
driver.execute_script(script)
btn_rechercher = driver.find_element_by_id('boutonr')
btn_rechercher.click()
print(driver.current_url)
r = requests.get(driver.current_url)
soup = BeautifulSoup(r.text, 'html.parser')
results = soup.find_all('div', attrs={'class':'bloc_titre'})
len(results)
records = []
for result in results:
nom = result.find('a').text
records.append((nom))
len(records)
import pandas as pd
df = pd.DataFrame(records, columns=['nom'])
df.head()
更多详情,这是主页: HomePage
这是我想要抓取的页面,然后我发送一个包含我的目的地和日期的表格: hotelList
我的代码输出显示主页列表而不是第二个的问题: Output
希望我现在说清楚了,谢谢。
【问题讨论】:
-
你会得到什么错误?
-
@CatChMeIfUCan 我没有弄错,它只是显示了错误的列表,它返回到主页而不是包含我要从中提取数据的列表的页面。
-
为什么不使用纯硒代替 bs4?
-
@CatChMeIfUCan 感谢您的回复,这是我第一次使用 python 我找到了这个解决方案,所以当它开始工作时我认为这是正确的解决方案我不知道要解决什么.
-
让我看看网站,我会为你写一个硒的
标签: python selenium selenium-webdriver web-scraping python-requests