Python:任何方法来进行Webscrape并检测单页应用程序中的更改



所以我正在尝试进行韦克斯克拉普并在网站上检查特定的更改,并且该网站有一个搜索栏,我需要在其中输入一些东西以进入我想在其中进行Webccrape的特定页面。问题是,该网站是一个单页应用程序,在此页面刷新了新结果后,URL不会更改。我尝试使用requests,但没有使用,因为它取决于URL ...

requests或Python库中是否有一个方法可以绕过此问题并让我继续前进?

我的建议是,尝试使用开发人员控制台打开页面。当您输入数据时,请检查水疗中心发送的什么样的请求(XHR请求是您的兴趣(。URL处理有效负载格式等,然后模仿网页。使用requests创建一个session对象,获取页面(这可能不是强制性的,但不会受到伤害,为什么不这样做(,然后将有效载荷发送到正确的地址,您将收到数据。可能不会是HTML和更多的JSON数据,但这甚至更好,因为以后更容易使用。如果您确实需要拥有HTML版本,则Python中有绑定与PhantomJS这样的库。您可以使用它们来渲染页面,然后检查是否存在特定元素。您也可以使用selenium是库,它允许您控制浏览器。您甚至可以观看它的工作。它使用您现有的浏览器,因此可以处理任何类型的网页水疗中心或其他网页。这完全取决于您的需求。如果您遵循纯数据,我将使用我的第一个解决方案,如果您想模仿用户,那么selenium是迄今为止最简单的。

以下示例使用硒,从其网站上发挥作用。

from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
# Create a new instance of the Firefox driver
driver = webdriver.Firefox()
# go to the google home page
driver.get("http://www.google.com")
# the page is ajaxy so the title is originally this:
print driver.title
# find the element that's name attribute is q (the google search box)
inputElement = driver.find_element_by_name("q")
# type in the search
inputElement.send_keys("cheese!")
# submit the form (although google automatically searches now without submitting)
inputElement.submit()
try:
    # we have to wait for the page to refresh, the last thing that seems to be updated is the title
    WebDriverWait(driver, 10).until(EC.title_contains("cheese!"))
    # You should see "cheese! - Google Search"
    print driver.title
finally:
    driver.quit()