代码之家  ›  专栏  ›  技术社区  ›  AlexT

Python-在网页上搜索仅在滚动后出现的信息

  •  0
  • AlexT  · 技术社区  · 7 年前

    我在努力刮 this web page

    我试图做的是一直滚动到页面底部,以便显示所有参数(到达页面底部不需要那么长时间),然后从那里提取html代码。

    这就是我所做的。我从你那里得到了滚动代码 here 顺便说一句

    SCROLL_PAUSE_TIME = 0.5
    
    #launch url
    url = 'https://en.arguman.org/fallacies'
    
    #create chrome sessioin
    driver = webdriver.Chrome()
    driver.implicitly_wait(30)
    driver.get(url)
    
    #get scroll height
    last_height = driver.execute_script("return document.body.scrollHeight")
    
    
    while True:
        # Scroll down to bottom
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    
        # Wait to load page
        time.sleep(SCROLL_PAUSE_TIME)
    
        # Calculate new scroll height and compare with last scroll height
        new_height = driver.execute_script("return document.body.scrollHeight")
        if new_height == last_height:
            break
        last_height = new_height
    
    http = urllib3.PoolManager()
    response = http.request('GET', url)
    soup = BeautifulSoup(response.data, 'html.parser')
    
    claims_h2 = soup('h2')
    claims =[]
    for c in claims_h2:
        claims.append(c.get_text())
    
    for c in claims:
        print (c)
    

    这就是我得到的,这是所有的参数,你会看到没有滚动,并有更多的添加到页面。

    Plants should have the right to vote.
    Plants should have the right to vote.
    Plants should have the right to vote.
    Postmortem organ donation should be opt-out
    Jimmy Kimmel should not bring up inaction on gun policy (now)
    A monarchy is the best form of government
    A monarchy is the best form of government
    El lenguaje inclusivo es innecesario
    Society suffers the most when dealing with people having mental disorders
    Illegally downloading copyrighted music and other files is morally wrong.
    

    基本上,我的代码似乎没有解析更新的html代码。

    1 回复  |  直到 7 年前
        1
  •  4
  •   cody    7 年前

    使用Selenium打开站点,进行所有滚动,然后使用Selenium再次发出请求是没有意义的 urllib . 这两个过程是完全独立和不相关的。

    相反,当滚动完成时,通过 driver.page_source 到 BeautifulSoup

    import time
    
    from bs4 import BeautifulSoup
    from selenium import webdriver
    
    driver = webdriver.Chrome()
    driver.implicitly_wait(30)
    
    try:
        SCROLL_PAUSE_TIME = 0.5
        driver.get("https://en.arguman.org/fallacies")
    
        last_height = driver.execute_script("return document.body.scrollHeight")
    
        while True:
            driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            time.sleep(SCROLL_PAUSE_TIME)
            new_height = driver.execute_script("return document.body.scrollHeight")
            if new_height == last_height:
                break
            last_height = new_height
    
        soup = BeautifulSoup(driver.page_source, "html.parser")
    
        for c in soup("h2"):
            print(c.get_text())
    
    finally:
        driver.quit()
    

    结果:

    Plants should have the right to vote.
    Plants should have the right to vote.
    Plants should have the right to vote.
    Postmortem organ donation should be opt-out
    Jimmy Kimmel should not bring up inaction on gun policy (now)
    A monarchy is the best form of government
    A monarchy is the best form of government
    El lenguaje inclusivo es innecesario
    Society suffers the most when dealing with people having mental disorders
    Illegally downloading copyrighted music and other files is morally wrong.
    Semi-colons are pointless in Javascript
    You can't measure how good a programming language is.
    You can't measure how good a programming language is.
    Semi-colons are pointless in Javascript
    Semi-colons are pointless in Javascript
    Semi-colons are pointless in Javascript
    ...
    
    推荐文章