【问题标题】:Scrapy - How to keep track of start urlScrapy - 如何跟踪起始网址
【发布时间】:2016-09-19 11:56:22
【问题描述】:

给定一个起始 url 池,我想在 parse_item() 函数中识别原始 url。

就我而言,scrapy 蜘蛛从初始的起始 url 池开始爬行,但是在解析时,没有任何迹象表明这些 url 中的哪一个是最初的。如何跟踪起点?

【问题讨论】:

    标签: python scrapy web-crawler


    【解决方案1】:

    如果您需要在蜘蛛内部解析 url,只需使用 response.url:

    def parse_item(self, response):
        print response.url 
    

    但如果您在蜘蛛之外需要它,我可以考虑以下方式:

    1. 使用scrapy core api
    2. 您还可以使用 OS 命令从外部 python 模块调用 scrapy(显然不推荐这样做):

    在scrapycaller.py中

    from subprocess import call
    urls = 'url1,url2'
    cmd = 'scrapy crawl myspider -a myurls={}'.format(urls)
    call(cmd, shell=True)
    

    myspider 内部:

    class mySpider(scrapy.Spider):
        def __init__(self, myurls=''):              
            self.start_urls = myurls.split(",") 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 2016-06-16
      • 1970-01-01
      • 2020-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多