【问题标题】:Issues scraping HTML code with Selenium WebDriver: Returned URL and action values different from the console value使用 Selenium WebDriver 抓取 HTML 代码的问题:返回的 URL 和操作值与控制台值不同
【发布时间】:2019-08-01 05:55:07
【问题描述】:

希望我能找到你。

作为 Python 和网络抓取的新手,我希望您能帮助我了解我在从事的项目中遇到的问题。

为了给您提供一些背景信息,我目前正在设计一个脚本,该脚本允许居住在法国的个人自动检查可用的时间段,以便为他们居住的地区的法国公民申请流程安排预约。由于时间段非常难以获得,并且根据用户反馈,时间段来来往往很快,因此该过程成为许多人不断沮丧的根源。

下面的代码摘自连接到学区网站、选择可用摊位并返回可用性页面的“action”参数的脚本。 time.sleep 函数用于避免网站使用的代理过载(因为它容易出现 502 错误),我正在打印当前 url 和操作值以验证它们是否与浏览器中的值对齐:

from selenium import webdriver
from bs4 import BeautifulSoup
import requests
import time

url = "http://www.hauts-de-seine.gouv.fr/booking/create/4462/1"
booth_selection = "//input[@value='7070'][@name='planning']"
booking_selector = "//input[@value='Etape suivante'][@name='nextButton']"
browser = webdriver.Safari()
browser.maximize_window()
browser.get(url)
time.sleep(5)

booth_selection = browser.find_element_by_xpath(booth_selection)
booth_selection.click()
time.sleep(5)
booking_submit = browser.find_element_by_xpath(booking_selector)
booking_submit.click()
browser.implicitly_wait(5)
page = browser.current_url
print(page)
agent = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'}
page_response = requests.get(page, headers=agent)
soup = BeautifulSoup(page_response.text, 'lxml')
action = soup.find('form', id='FormBookingCreate').get('action')
print(action)
time.sleep(10)
browser.close()

不幸的是,情况似乎并非如此,因为:

  • 作为 URL,我仍然得到相同的起始 URL,而不是“http://www.hauts-de-seine.gouv.fr/booking/create/4462/2
  • 对于操作值,我得到的是“/booking/create/4462/1”而不是“/booking/create/4462/2”,这导致我假设我将无法抓取结果的 HTML网址。

这是 Safari 的控制台截图供参考:HTML code of resulting page

您能帮我理解为什么会这样吗?可以做些什么来解决这个问题?

提前致谢。

【问题讨论】:

  • 为什么使用请求?你不能直接把 browser.page_source 传给 BeautifulSoup 吗?

标签: python selenium selenium-webdriver web-scraping scripting


【解决方案1】:

不断收到 504 错误,尽管您需要进行一些挖掘......这是因为 cookie 无法直接访问其他 URL。使用 selenium 会很慢,我建议只使用 request 来处理这些事情。

import requests 
    headders = {"Host": "www.hauts-de-seine.gouv.fr",
    "Connection": "keep-alive",
    "Cache-Control": "max-age=0",
    "Upgrade-Insecure-Requests": 1,
    "DNT": 1,
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    "Referer": "http://www.hauts-de-seine.gouv.fr/booking/create/4462/1",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "en,en-GB;q=0.9,id;q=0.8",
    "Cookie": "eZSESSID={theCookie}; xtvrn=${AnotherCookie}$;{AnotherCookie}=-; {AnotherCookie}=1"} 

html = requests.get("http://www.hauts-de-seine.gouv.fr/booking/create/4462/2", headers=headders) 

【讨论】:

  • 504 错误可能是由于网站的请求过载。也就是说,您提供的代码似乎不起作用(在 PyCharm 上测试),因为它不返回下一页的 HTML。此外,对于“Upgrade-Insecure-Request”和“DNT”的值,它们应该是 str 格式,即用引号括起来。但是感谢您的建议:)
【解决方案2】:

我想这就是你要找的东西:-

from selenium import webdriver
from bs4 import BeautifulSoup
import requests
import time
url = "http://www.hauts-de-seine.gouv.fr/booking/create/4462/1"
options = webdriver.ChromeOptions()
options.add_argument('start-maximized')
options.add_argument('disable-infobars')
options.add_argument('--disable-extensions')
browser = webdriver.Chrome(chrome_options=options,executable_path=r'\\chromedriver')
browser.get(url)
browser.find_element_by_class_name("Bligne").click()
browser.find_element_by_class_name("Bbutton").click()
page = browser.current_url
print(page)
html = browser.page_source
soup = BeautifulSoup(html, 'lxml')
action = soup.find(id = 'FormBookingCreate').get('action')
print(action)

我得到的输出是:-

http://www.hauts-de-seine.gouv.fr/booking/create/4462/2

'/booking/create/4462/2'

所以我所做的是使用类名和 id 来提取所需的信息。如果你愿意,你可以忽略选项部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    相关资源
    最近更新 更多