我正试图抓取一个有无限滚动页面的新闻页面(thenextweb.com)
我已经写了一个函数来滚动,但它需要太多的时间滚动。我不得不使用time.sleep()
,因为我的互联网连接很弱,有时间加载新页面
这是我的向下滚动功能,我使用了此问题的解决方案:https://stackoverflow.com/questions/20986631/how-can-i-scroll-a-web-page-using-selenium-webdriver-in-python"
def scrolldown(urltoscroll):
browser.get(urltoscroll)
last_height = browser.execute_script("return document.body.scrollHeight")
next_button = browser.find_element_by_xpath('//*[@id="channelPaginate"]')
while True:
browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(6)
next_button.click()
time.sleep(8)
new_height = browser.execute_script("return document.body.scrollHeight")
time.sleep(6)
if new_height == last_height:
break
last_height = new_height
有没有其他方法可以更轻松地处理这些类型的页面
多谢各位
编辑:我要刮取的链接:https://thenextweb.com/plugged/". 我想得到文章hrefs
下面是一个示例selenium代码段,您可以将其用于此类型的目的。它会转到“Enumerate python tutorial”(枚举python教程)上youtube搜索结果的url并向下滚动,直到找到标题为“Enumerate python tutorial(2020)”的视频
您还可以将其应用于新闻抓取代码
嗯,向下滚动操作似乎触发了一个API调用,您可以使用
requests
模块模拟该调用来加载每个页面以下是最新新闻部分的示例:
输出:
相关问题 更多 >
编程相关推荐