我正在尝试一个易碎的包装https://github.com/clemfromspace/scrapy-selenium。
我已经按照上面github主页上的说明进行操作。我开始了一个新的scrapy项目并创建了一个蜘蛛:
from scrapy_selenium import SeleniumRequest
from shutil import which
SELENIUM_DRIVER_NAME = 'firefox'
# SELENIUM_DRIVER_EXECUTABLE_PATH = which('chromedriver')
SELENIUM_DRIVER_EXECUTABLE_PATH = which('geckodriver')
SELENIUM_DRIVER_ARGUMENTS=['-headless'] # '--headless' if using chrome instead of firefox
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:48.0) Gecko/20100101 Firefox/48.0'
class MySpider(scrapy.Spider):
start_urls = ["http://yahoo.com"]
name = 'test'
def start_requests(self):
for url in self.start_urls:
yield SeleniumRequest(url, self.parse_index_page)
def parse_index_page(self, response):
....
输出:
'scrapy.extensions.telnet.TelnetConsole',
'scrapy.extensions.logstats.LogStats']
2019-07-05 14:14:44 [scrapy.middleware] WARNING: Disabled SeleniumMiddleware: SELENIUM_DRIVER_NAME and SELENIUM_DRIVER_EXECUTABLE_PATH must be set
2019-07-05 14:14:44 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
........
'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
'scrapy.downloadermiddlewares.stats.DownloaderStats']
2019-07-05 14:14:44 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
'scrapy.spidermiddlewares.referer.RefererMiddleware',
'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
'scrapy.spidermiddlewares.depth.DepthMiddleware']
2019-07-05 14:14:44 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2019-07-05 14:14:44 [scrapy.core.engine] INFO: Spider opened
2019-07-05 14:14:44 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2019-07-05 14:14:44 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2019-07-05 14:14:44 [scrapy.core.engine] ERROR: Error while obtaining start requests
Traceback (most recent call last):
File "....\splashtest\lib\site-packages\scrapy\core\engine.py", line 127, in _next_request
request = next(slot.start_requests)
File "E:\ENVS\r3\scrapySelenium\scrapySelenium\spiders\test.py", line 55, in start_requests
yield SeleniumRequest(url, self.parse_index_page)
File "....\splashtest\lib\site-packages\scrapy_selenium\http.py", line 32, in __init__
super().__init__(*args, **kwargs)
TypeError: __init__() missing 1 required positional argument: 'url'
2019-07-05 14:14:44 [scrapy.core.engine] INFO: Closing spider (finished)
2019-07-05 14:14:44 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'finish_reason': 'finished',
'finish_time': datetime.datetime(2019, 7, 5, 18, 14, 44, 74274),
'log_count/ERROR': 1,
'log_count/INFO': 9,
'log_count/WARNING': 1,
'start_time': datetime.datetime(2019, 7, 5, 18, 14, 44, 66256)}
2019-07-05 14:14:44 [scrapy.core.engine] INFO: Spider closed (finished)
我在做什么错了?
答案 0 :(得分:1)
从问题的链接中,scrapy_selenium.SeleniumRequest
构造函数采用初始参数wait_time
,wait_until
,screenshot
和script
,并将所有剩余参数传递给到scrapy.Request
构造函数。
发布的代码实例化具有两个位置参数的SeleniumRequest
,这意味着没有参数将传递给Request
构造函数,从而引起问题。
要解决此问题,您可以按位置明确传递默认的SeleniumRequest
参数:
yield SeleniumRequest(None, None, False, None, url, self.parse_index_page)
或通过关键字传递Request
参数(通常是一种更好的样式,既因为它可以抵抗对默认参数的更改,又因为它更容易确定正在发生的事情):
yield SeleniumRequest(url=url, callback=self.parse_index_page)