Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript python selenium phantomjs无休止的滚动仅适用于第一页_Javascript_Python_Selenium_Web Scraping_Phantomjs - Fatal编程技术网

Javascript python selenium phantomjs无休止的滚动仅适用于第一页

Javascript python selenium phantomjs无休止的滚动仅适用于第一页,javascript,python,selenium,web-scraping,phantomjs,Javascript,Python,Selenium,Web Scraping,Phantomjs,我正在尝试使用python和phantomjs阅读一些新闻文章。 我正在使用无休止滚动的网站在滚动到底部时动态加载下一篇文章。是一个示例URL 我设法使用下面的代码让它工作,以加载一个以上的文章,但只有一个以上。。。有人能帮我让它无休止地工作吗?或者任何提示出了什么问题,可以改进吗? 谢谢大家! from selenium import webdriver from bs4 import BeautifulSoup from time import sleep from selenium.web

我正在尝试使用python和phantomjs阅读一些新闻文章。 我正在使用无休止滚动的网站在滚动到底部时动态加载下一篇文章。是一个示例URL

我设法使用下面的代码让它工作,以加载一个以上的文章,但只有一个以上。。。有人能帮我让它无休止地工作吗?或者任何提示出了什么问题,可以改进吗? 谢谢大家!

from selenium import webdriver
from bs4 import BeautifulSoup
from time import sleep
from selenium.webdriver.common.proxy import *
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

# Pretend to be chrome
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = (
    "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53 "
    "(KHTML, like Gecko) Chrome/15.0.87"
)

driver = webdriver.PhantomJS(desired_capabilities=dcap)
driver.set_window_size(1120, 550)

## GET
driver.get("https://www.bloomberg.com/news/features/2017-06-08/no-one-has-ever-made-a-corruption-machine-like-this-one")

# print current scrollTop
driver.execute_script('return document.body.scrollTop')
# out: 0

# print current scrollHeight
driver.execute_script('return document.body.scrollHeight')
# out: 18255

# scroll to bottom
driver.execute_script("window.scrollTo(0, 18255)")

# print current scrollTop
driver.execute_script('return document.body.scrollTop')
# out: 17705

# print current scrollHeight 
driver.execute_script('return document.body.scrollHeight')
# out: 29050
# It works! Great!

# Scroll to bottom again
driver.execute_script("window.scrollTo(0, 29050)")

# print current scrollTop
driver.execute_script('return document.body.scrollTop')
# out: 28500

# print current scrollHeight 
driver.execute_script('return document.body.scrollHeight')
# out: 29050
# It's still the same, no matter how hard I try, it cannot load more... 


# According to tolmachofof's suggestion below, I tried to scroll very slowly, still no luck. :<
top = driver.execute_script('return document.body.scrollTop')
height = driver.execute_script('return document.body.scrollHeight')
for i in range(top, height, 100):
    driver.execute_script("window.scrollTo(0," + str(i) + ")")
    print(driver.execute_script('return document.body.scrollTop'))
    sleep(0.2)
从selenium导入webdriver
从bs4导入BeautifulSoup
从时间上导入睡眠
从selenium.webdriver.common.proxy导入*
从selenium导入webdriver
从selenium.webdriver.common.desired_功能导入DesiredCapabilities
#冒充
dcap=dict(DesiredCapabilities.PHANTOMJS)
dcap[“phantomjs.page.settings.userAgent”]=(
“Mozilla/5.0(X11;Linux x86_64)AppleWebKit/53”
“(KHTML,如壁虎)铬/15.0.87”
)
driver=webdriver.PhantomJS(所需的功能=dcap)
驱动程序。设置窗口大小(1120550)
##得到
驱动程序。获取(“https://www.bloomberg.com/news/features/2017-06-08/no-one-has-ever-made-a-corruption-machine-like-this-one")
#打印当前滚动条
driver.execute_脚本('returndocument.body.scrollTop'))
#输出:0
#打印当前滚动高度
driver.execute_脚本('returndocument.body.scrollHeight'))
#电话:18255
#滚动到底部
驱动程序.execute_脚本(“window.scrollTo(018255)”)
#打印当前滚动条
driver.execute_脚本('returndocument.body.scrollTop'))
#输出:17705
#打印当前滚动高度
driver.execute_脚本('returndocument.body.scrollHeight'))
#输出:29050
#它起作用了!伟大的
#再次滚动到底部
执行_脚本(“window.scrollTo(029050)”)
#打印当前滚动条
driver.execute_脚本('returndocument.body.scrollTop'))
#电话:28500
#打印当前滚动高度
driver.execute_脚本('returndocument.body.scrollHeight'))
#输出:29050
#它仍然是一样的,无论我怎么努力,它不能加载更多。。。
#根据托尔马霍夫在下面的建议,我试图非常缓慢地滚动,但仍然没有运气<
top=driver.execute_脚本('returndocument.body.scrollTop'))
高度=驱动程序.execute_脚本('return document.body.scrollHeight'))
对于范围内的i(顶部、高度、100):
驱动程序.execute_脚本(“window.scrollTo(0,+str(i)+”))
打印(driver.execute_脚本('returndocument.body.scrollTop'))
睡眠(0.2)

您可以使用以下脚本:

    SCROLL_TEMPLATE = """

        var scroll_interval = arguments[0];
        var scroll_time = arguments[1];
        var scroll_step = arguments[2]

        function scroll() {
            document.body.scrollTop += scroll_step;
        }

        var _scroll = setInterval(scroll, scroll_interval)
        setTimeout(function() {clearInterval(_scroll)}, scroll_time)"""

    def scroll_page(driver, scroll_interval=0.5, scroll_time=5000, scroll_step=50):
        driver.execute_script(SCROLL_TEMPLATE, scroll_interval, scroll_time, scroll_step)
        # Script will finish before scroll if you delete it
        sleep((scroll_time / 1000) + 0.3)
注意:scroll\u interval是单个scroll语句之间的超时。
Scroll\u time是页面滚动的总时间。滚动步骤-单滚动步骤(px)

请阅读我的问题,我可以让它滚动,但我不知道为什么它只在第一页上工作…你的滚动速度非常快。我曾经有过同样的问题。这个解决方案通过降低滚动速度帮助我无休止地翻页。您使用了哪个滚动时间参数?我在这个页面中使用了scroll_time=50000-它可以工作。我用默认的婴儿车尝试了你的代码,也尝试了每0.2秒滚动100px,请参阅我更新的问题。两者都不起作用(这很奇怪=(我执行的代码是:scroll\u page(driver,scroll\u time=50000)。在本例中,print driver.execute\u script('return document.body.scrollHeight')是135957