当前位置: 首页 > 面试题库 >

无限滚动刮擦网站

谢奇略
2023-03-14
问题内容

我已经写了很多刮板,但是我不确定如何处理无限滚动条。如今,大多数网站,Facebook,Pinterest等都有无限滚动条。


问题答案:

您可以使用硒来刮除Twitter或Facebook之类的无限滚动网站。

步骤1:使用pip安装Selenium

pip install selenium

第2步:使用下面的代码自动执行无限滚动并提取源代码

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import sys

import unittest, time, re

class Sel(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "https://twitter.com"
        self.verificationErrors = []
        self.accept_next_alert = True
    def test_sel(self):
        driver = self.driver
        delay = 3
        driver.get(self.base_url + "/search?q=stckoverflow&src=typd")
        driver.find_element_by_link_text("All").click()
        for i in range(1,100):
            self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            time.sleep(4)
        html_source = driver.page_source
        data = html_source.encode('utf-8')


if __name__ == "__main__":
    unittest.main()

步骤3:根据需要打印数据。



 类似资料:
  • 我最近问了一个问题(这里引用:Python Web Scring(Beautiful Soup、Selenium和PhantomJS):只刮整页的一部分),这有助于确定我在滚动时动态更新的页面上刮所有内容时遇到的问题。然而,我仍然无法使用selenium来使用代码指向正确的元素,并迭代地向下滚动页面。我还发现,当我手动向下滚动页面时,有一些原始内容在页面加载时消失,而新内容则更新。例如,看下面的图

  • 我需要刮http://www.vintagetoday.be/fr/montres但它有动态内容。 我该怎么做呢? 从bs4导入请求导入美丽汤t=requests.get("vintagetoday.be/fr/catalogue.awp"). text print(len(美丽汤(t,"lxml"). findAll("td",{"class":"Lien2"}))

  • 你好,我想使用python 3.x和BeautifulSoup从一个带有年龄验证弹出式窗口的网站上刮取数据。如果不点击“是”来表示“你是否超过21岁”,我就无法获得底层的文本和图像。谢谢你的支持。 编辑:谢谢,通过一些注释的帮助,我看到我可以使用cookie,但不确定如何管理/存储/调用请求包中的cookie。 编辑4:取得了一些进展,更新的代码如下。我设法在XML代码中找到了元素,现在我只需要设

  • 无限滚动用来在页面滚动到接近底部时加载新内容或进行其他操作。 在底部的无限滚动 你只需在可滚动的容器上添加“infinite-scroll”类,一般是页面滚动区域 - div.content <style type="text/css"> .infinite-scroll-preloader { margin-top:-20px; } </style> <heade

  • 无限滚动用来在页面滚动到接近底部时加载新内容或进行其他操作。 无限滚动HTML结构 你只需在可滚动的容器上添加“infinite-scroll”类,一般是页面滚动区域 - <div class="page-content">: <div class="page"> <div class="page-content infinite-scroll" data-distance="100">

  • 但是我不想做一个循环,而是想触发一个事件,比如,如果用户手动按下load more Post按钮,新页面被加载,我得到页面的页面源。有什么办法可以做到吗?如有任何帮助,不胜感激。