【发布时间】: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