栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

无限滚动刮擦网站

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

无限滚动刮擦网站

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

步骤1:使用pip安装Selenium

pip install selenium

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

from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support.ui import Selectfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.common.exceptions import TimeoutExceptionfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.common.exceptions import NoSuchElementExceptionfrom selenium.common.exceptions import NoalertPresentExceptionimport sysimport unittest, time, reclass 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.enpre('utf-8')if __name__ == "__main__":    unittest.main()

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/617587.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号