我想获得与此命令行相同的结果: scrapy crawl linkedin_anonymous -a first = James -a last = Bond -o output.json
我的脚本如下:
import scrapy
from linkedin_anonymous_spider import LinkedInAnonymousSpider
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
spider = LinkedInAnonymousSpider(None, "James", "Bond")
process = CrawlerProcess(get_project_settings())
process.crawl(spider) ## <-------------- (1)
process.start()
我发现(1)中的process.crawl()正在创建另一个LinkedInAnonymousSpider,其中第一个和最后一个是None(打印在(2)中),如果是这样,那么就没有创建对象蜘蛛的意义了可以先将参数传递给process.crawl()吗?
linkedin_anonymous:
from logging import INFO
import scrapy
class LinkedInAnonymousSpider(scrapy.Spider):
name = "linkedin_anonymous"
allowed_domains = ["linkedin.com"]
start_urls = []
base_url = "https://www.linkedin.com/pub/dir/?first=%s&last=%s&search=Search"
def __init__(self, input = None, first= None, last=None):
self.input = input # source file name
self.first = first
self.last = last
def start_requests(self):
print self.first ## <------------- (2)
if self.first and self.last: # taking input from command line parameters
url = self.base_url % (self.first, self.last)
yield self.make_requests_from_url(url)
def parse(self, response): . . .
答案 0 :(得分:1)
您可以轻松地做到这一点:
from scrapy import cmdline
cmdline.execute("scrapy crawl linkedin_anonymous -a first=James -a last=Bond -o output.json".split())
答案 1 :(得分:1)
如果您有Scrapyd,并且想安排蜘蛛,请执行此操作
curl http://localhost:6800/schedule.json -d project=projectname -d spider=spidername -d first='James' -d last='Bond'