python-2.7 - 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 数量
解决方案
推荐阅读
- r - 将预测值添加到原始 df
- python - 如何更改 kivy Graph 的背景颜色?
- javascript - 从输入中检索数据 - laravel
- xslt - 按类型分组的 xslt 字段
- java - 如何使用 CSS 选择器在元素之间进行选择
- error-handling - 无法在 Gerrit 中加载插件事件日志
- mongodb - 在 MongoDB 中获取文档的一部分
- android - 发生内部错误。[ MISSING_CLIENT_IDENTIFIER ]
- javascript - html5 文件输入 - 如何使用 javascript 更改文件文本的颜色?
- c# - 将大量数据从 DB 写入 CSV 文件的快速有效方法