【问题标题】:How to fetch data from infinite scrolling webpage using BeautifulSoup and Selenium如何使用 BeautifulSoup 和 Selenium 从无限滚动网页中获取数据
【发布时间】:2021-01-02 05:57:21
【问题描述】:

我正在尝试获取“https://uynaa.wordpress.com/category/%d0%be%d1%80%d1%87%d1%83%d1%83%d0%bb”下的所有文章链接%d0%b3%d1%8b%d0%bd-%d0%bd%d0%b8%d0%b9%d1%82%d0%bb%d1%8d%d0%bb/"。问题是我的代码无法获取链接下的所有文章,因为它是可滚动的。我找到了这个叫做 selenium 的包,但是仍然使用 selenium,我的代码没有找到所有的文章。

import os
import requests
from lxml import html
from bs4 import BeautifulSoup
from urllib.request import urlopen
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Chrome(executable_path="./chromedriver")
browser.get("https://uynaa.wordpress.com/category/%d0%be%d1%80%d1%87%d1%83%d1%83%d0%bb%d0%b3%d1%8b%d0%bd-%d0%bd%d0%b8%d0%b9%d1%82%d0%    bb%d1%8d%d0%bb/")

body = browser.find_element_by_tag_name("body")

no_of_pagedowns = 20

while no_of_pagedowns:
    body.send_keys(Keys.PAGE_DOWN)
    no_of_pagedowns-=1


html = urlopen("https://uynaa.wordpress.com/category/%d0%be%d1%80%d1%87%d1%83%d1%83%d0%bb%d0%b3%d1%8b%d0%bd-%d0%bd%d0%b8%d0%b9%d1%82%d0%bb%d1%8d%d0%bb/").read()
soup = BeautifulSoup(html, "lxml")


for element in soup.findAll('div', {'class': 'blog'}):
    for link in element.findAll('h2'):
        for alink in link.findAll('a'):
            try:
                print (alink['href'])
            except KeyError:
                pass

如何获取此网址下的所有文章链接?

【问题讨论】:

    标签: python python-3.x selenium selenium-webdriver beautifulsoup


    【解决方案1】:

    selenium 中有一个向下滚动站点的功能。然后只需等待几秒钟,例如time.sleep(2) 然后你可以抓取下一个 div 标签的内容。只需将其放入循环中即可。

    【讨论】:

      猜你喜欢
      • 2019-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      • 1970-01-01
      • 2015-05-06
      • 1970-01-01
      相关资源
      最近更新 更多