有:
from twisted.internet import reactor
from scrapy.crawler import CrawlerProcess
Run Code Online (Sandbox Code Playgroud)
我总是成功地运行这个过程:
process = CrawlerProcess(get_project_settings())
process.crawl(*args)
# the script will block here until the crawling is finished
process.start()
Run Code Online (Sandbox Code Playgroud)
但是因为我已将此代码移动到web_crawler(self)函数中,如下所示:
def web_crawler(self):
# set up a crawler
process = CrawlerProcess(get_project_settings())
process.crawl(*args)
# the script will block here until the crawling is finished
process.start()
# (...)
return (result1, result2)
Run Code Online (Sandbox Code Playgroud)
并开始使用类实例化调用该方法,如:
def __call__(self):
results1 = test.web_crawler()[1]
results2 = test.web_crawler()[0]
Run Code Online (Sandbox Code Playgroud)
和运行:
test()
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Traceback (most recent call last):
File "test.py", line 573, in <module> …Run Code Online (Sandbox Code Playgroud) 我在启动同一个蜘蛛的多个实例时遇到困难.我想像1个url for 1 spider实例一样运行它.我必须处理50k网址,为此我需要为每个网址启动单独的实例.在我的主蜘蛛脚本中,我设置了closepider timeut 7分钟,以确保我不会长时间爬行.请参阅以下代码:
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
import urlparse
for start_url in all_urls:
domain = urlparse.urlparse(start_url).netloc
if domain.startswith('ww'):
domain = domain.split(".",1)[1]
process = CrawlerProcess(get_project_settings())
process.crawl('textextractor', start_url=start_url,allowed_domains=domain)
process.start()
Run Code Online (Sandbox Code Playgroud)
它完全运行第一个url,bur之后第二个url通过时它给出了以下错误:
raise error.ReactorNotRestartable()
ReactorNotRestartable
Run Code Online (Sandbox Code Playgroud)
请建议我该怎么做才能让它运行同一个蜘蛛的多个实例.此外,我正在考虑使用线程一次启动多个scrapy实例.这是一个很好的方法吗?
我需要的:
我试试这个:
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
from time import sleep
while True:
process = CrawlerProcess(get_project_settings())
process.crawl('spider_name')
process.start()
sleep(60)
Run Code Online (Sandbox Code Playgroud)
但得到错误:
twisted.internet.error.ReactorNotRestartable
请帮我做对
Python 3.6
Scrapy 1.3.2
Linux
所以我一直在使用硒来进行刮擦。但我想将所有代码更改为 Scrapy。我唯一不确定的是我正在使用多处理(python 库)来加速我的进程。我研究了很多,但我完全不明白。我发现:Multiprocessing of Scrapy Spiders in Parallel Processes但它对我没有帮助,因为它说它可以用 Twisted 完成,但我还没有找到一个例子。
在其他论坛上,它说 Scrapy 可以与多处理一起工作。
最后一件事,在scrapy中,选项CONCURRENT_REQUESTS(设置)与多处理有一些联系吗?
我目前正在尝试让 scrapy 在 Google Cloud Function 中运行。
from flask import escape
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
def hello_http(request):
settings = get_project_settings()
process = CrawlerProcess(settings)
process.crawl(BlogSpider)
process.start()
return 'Hello {}!'.format(escape("Word"))
Run Code Online (Sandbox Code Playgroud)
这有效,但奇怪的是,不是“一直”。每隔一段时间,HTTP 调用就会返回一个错误,然后我可以在堆栈驱动程序上读取:
Function execution took 509 ms, finished with status: 'crash'
我检查了蜘蛛,甚至将其简化为不会失败的东西,例如:
import scrapy
class BlogSpider(scrapy.Spider):
name = 'blogspider'
start_urls = ['https://blog.scrapinghub.com']
def parse(self, response):
yield { 'id': 1 }
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释一下发生了什么事吗?
这可能是我达到的资源配额吗?