在Scrapy python中向process.crawl传递参数

2izufjch  于 2023-06-23  发布在  Python
关注(0)|答案(4)|浏览(154)

我希望得到与此命令行相同的结果: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,其中first和last都是None(打印在(2)中),如果是这样,那么没有必要创建对象蜘蛛,并且如何将参数first和last传递给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): . . .
w3nuxt5m

w3nuxt5m1#

process.crawl方法上传递spider参数:

process.crawl(spider, input='inputargument', first='James', last='Bond')
t8e9dugd

t8e9dugd2#

你可以用简单的方法来做:

from scrapy import cmdline

cmdline.execute("scrapy crawl linkedin_anonymous -a first=James -a last=Bond -o output.json".split())
pvcm50d1

pvcm50d13#

如果你有Scrapyd并且你想安排蜘蛛,那么这样做
curl http://localhost:6800/schedule.json -d project=projectname -d spider=spidername -d first='James' -d last='Bond'

ckx4rj1h

ckx4rj1h4#

试试这个:

import os

first_name = "DemoFirstName"
last_name = "DemoLastName"

os.system(f"""scrapy crawl linkedin_anonymous \
                      -a first={first_name} \
                      -a last={last_name} \
                      -o output.json""")

不要在=之间放置任何空格。

相关问题