首页 > 解决方案 > scrapy 定义(自我、响应)是否受到限制或需要特殊功能才能在循环中解析多个 URL?

问题描述

我正在解析两个 URL 列表,后者是从第一个填充的。我能够获取所有需要的 URL,但无法从最后一个 URL 列表中提取数据。我只能获取一个 URL 的数据

我尝试过使用 scrapy.Request、response.follow、While 语句,但只能从一个 URL 获得响应。我是scrapy/python的新手,不知道如何解决这个问题。

import scrapy



class DBSpider(scrapy.Spider):
 name = "Scrape"
 allowed_domains = ["192.168.3.137"]
 start_urls = [
 'http://192.168.3.137/api/?controller_id=' + str(x)
 for x in range(0,8)
 ]

 def parse(self, response):
  for sites in response.xpath('//* 
   [@id="siteslist"]//li/a/@href').extract(): 
   yield response.follow(sites, self.parse_sites)

 def parse_sites(self, response):
  for device_pages in response.xpath('//* 
   [@id="list_devices"]/a/@href').extract():
   yield scrapy.Request(response.urljoin(device_pages), 
   self.parse_macs)

 def parse_macs(self, response):
  print response.url #only gets one response url

parse_mac 只打印一个响应 url。应该等于 parse_sites def 中 devices_pages 循环中的 url 数量

标签: python-2.7scrapy

解决方案


推荐阅读