python - 我的蜘蛛对我的需要来说太慢了,我不知道如何解决它
问题描述
我是数据抓取的初学者,我想创建一个蜘蛛,它可以检索本地网站上的所有可用工作,给定关键字和城市。
我还想从每个工作中检索数据,所以我必须遵循该链接。
我的代码有两个问题,我不知道如何解决(在自己尝试了半天之后) - 每个请求需要 7 秒,这非常慢(如果我有 1-2k 个作业要抓取.. . 那就太多了)。
我已经在网上搜索了如何修复它,并修复了我的选择器——现在我认为我只使用了最低限度的选择器,而且还使用了更快的选择器。我也开始使用显式等待而不是隐式等待 - 现在每个请求有 7 秒,但我不知道如何减少它。
另外,我想抓取每个链接,但如果有的话,我也想进入下一页。这就是为什么我的 parse 方法中有两个 yield 语句,但我的方法不起作用。我没有得到下一页,我想。
# -*- coding: utf-8 -*-
import scrapy
from scrapy.spiders.init import InitSpider
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from scrapy.selector import Selector
from scrapy_selenium import SeleniumRequest
class ExampleSpider(InitSpider):
name = 'example'
def init_request(self):
yield SeleniumRequest(
url='https://www.ejobs.ro/',
wait_time=3,
callback=self.search
)
return self.initialized()
def search(self, response):
driver = response.meta['driver']
search_input = driver.find_element_by_xpath("//input[@id='keyword']")
search_input.send_keys("programator")
search_input2 = driver.find_element_by_xpath("//input[@id='s2id_autogen1']")
search_input2.send_keys("bucuresti")
selectieOras = driver.find_element_by_xpath("//input[@id='s2id_autogen1_search']")
selectieOras.send_keys(Keys.ENTER)
submit = driver.find_element_by_xpath("//button[@id='submit']")
driver.execute_script("arguments[0].click();", submit)
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "searchSection"))
)
finally:
yield SeleniumRequest(
url=driver.current_url,
wait_time=3,
callback=self.parse
)
def parse(self, response):
driver = response.meta['driver']
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "searchSection"))
)
finally:
html = driver.page_source
response_obj = Selector(text=html)
links = response_obj.xpath("//div[@class='jobitem-body']")
for link in links:
URL = link.xpath(".//a[contains(@class, 'title')]/@href").get()
if URL:
yield SeleniumRequest(
url=URL,
wait_time=3,
callback=self.parse_res
)
next = response_obj.xpath("//div[@id='searchPagination']/li[@class='next']/a/@href")
if next:
hrefLink = next.get()
yield SeleniumRequest(
url=hrefLink,
wait_time=3,
callback=self.parse
)
def parse_res(self, response):
yield {
'title': response.xpath("//h1[@class='jobad-title']/text()").get()
}
有没有办法解决这两个问题?我是网络抓取的完全初学者,我已经尝试了我在文档和网上能找到的所有东西,我没有其他想法。
谢谢。
解决方案
- 为什么你使用 selenium 请求而不是 scrapy 请求你
需要渲染 java 脚本吗? - 为什么你使用 wait_time=3 ?你真的需要等那么久吗?
- 检查您是否需要禁用自动油门?
- 可能您还需要在设置中增加并发请求
推荐阅读
- javascript - 通过 Javascript 从用户那里获取所有脚本
- javascript - React js:音频元素的 src 在 setState 上更新,但 audioRef 没有
- excel - XML 不可导出的 edigas
- solr - solr:copyField 之后不可搜索的文本索引和存储
- python-3.x - 无法在串行 Python 中发送(或不接收)0 值
- python - 学习 Python BeautifulSoup,为什么我必须在 For 循环中使用两个元素?
- reactjs - 使用节点媒体服务器托管 RTMP 服务器
- typo3 - 错字3 | powermail - 保存和使用会话数据(预填字段)
- firebase - 如何根据我之前发送的数据获取firebase数据
- tensorflow - ResNet 模型在加载到 Flask Web 应用程序时错误地预测一个类?不匹配保存的模型预测