python - 如何使用scrapy和selenium解析加载缓慢的网页?
问题描述
以下是我尝试过的:
import scrapy
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from scrapy_selenium import SeleniumRequest
class PdfxSpider(scrapy.Spider):
name = 'pdf'
urls = 'https://www.pdfdrive.com/living-in-the-light-a-guide-to-personal-transformation-d10172273.html'
def start_requests(self):
yield SeleniumRequest(
url=self.urls,
callback=self.parse,
#wait_time=1000,
wait_until=EC.element_to_be_clickable((By.ID, 'alternatives'))
)
def parse(self, response):
print(response.css('a.btn-success').xpath('@href').get())
解决方案
我会尝试使用 requests 和 BeautifulSoup
像这样的东西会给你类似的链接,而且速度很快。
import requests
from bs4 import BeautifulSoup
url = 'https://www.pdfdrive.com/living-in-the-light-a-guide-to-personal-transformation-d10172273.html'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
}
response = requests.get(url, headers=headers)
html = response.text
soup = BeautifulSoup(html, 'lxml')
links = soup.find_all('a', {"class":"ai-similar"})
for link in links:
print(link['href'])
推荐阅读
- python-3.x - 使用 winreg 获取软件(Acrobat Reader)的安装路径
- c# - .NET Entity Framework 在 VS 调试中工作,但在运行时无法连接到数据库
- angular - 如何配置 ag-grid 以显示在最后一页空行以填充它?
- mysql - 有没有办法在没有 php 脚本的情况下进行 msqyl 选择、插入、更新
- android - 无法加载类“org.jetbrains.plugins.gradle.tooling.ModelBuilderService”
- shell - 如何使用 SHELL 脚本覆盖 EnvInject 插件变量
- ios - 将pdf文档保存到iOS Files App时如何指定文件名?
- python - 为什么我可以在 Python 中打开超过 1024 个套接字?
- javascript - 我加载了完整的 jQuery 库,但我只使用 Ajax 和选择器。更好的短代码?
- android - 如何使用 REST API 检索 woocommerce 产品数据?