python - 基于https://github.com/scrapy/quotesbot/blob/master/quotesbot/spiders/toscrape-xpath.py的简单scrapy不使用yield Request传递数据
问题描述
强文本我基于搜索示例的代码似乎没有按预期运行,因此我决定使用在 github 上找到的工作模型:https ://github.com/scrapy/quotesbot/blob/master/quotesbot/spiders/toscrape -xpath.py 然后我稍微修改它以展示我遇到的问题。下面的代码按预期工作得很好,但我的最终目标是将抓取的数据从第一个“parse”传递到第二个“parse2”函数,以便我可以组合来自 2 个不同页面的数据。但是现在我想从非常简单的开始,这样我就可以了解正在发生的事情,因此下面的代码被大量剥离。
# -*- coding: utf-8 -*-
import scrapy
from quotesbot.items import MyItems
from scrapy import Request
class ToScrapeSpiderXPath(scrapy.Spider):
name = 'toscrape-xpath'
start_urls = [
'http://quotes.toscrape.com/',
]
def parse(self, response):
item = MyItems()
for quote in response.xpath('//div[@class="quote"]'):
item['tinfo'] =
quote.xpath('./span[@class="text"]/text()').extract_first()
yield item
but then when I modify the code as below:
# -*- coding: utf-8 -*-
import scrapy
from quotesbot.items import MyItems
from scrapy import Request
class ToScrapeSpiderXPath(scrapy.Spider):
name = 'toscrape-xpath'
start_urls = [
'http://quotes.toscrape.com/',
]
def parse(self, response):
item = MyItems()
for quote in response.xpath('//div[@class="quote"]'):
item['tinfo'] =
quote.xpath('./span[@class="text"]/text()').extract_first()
yield Request("http://quotes.toscrape.com/",
callback=self.parse2, meta={'item':item})
def parse2(self, response):
item = response.meta['item']
yield item
我只刮掉了一件,它说其余的都是重复的。看起来甚至根本没有读取“parse2”。我玩过缩进和括号,认为我错过了一些简单的东西,但没有多大成功。我查看了许多示例,看看我是否能理解可能是什么问题,但我仍然无法让它发挥作用。我相信对于那些大师来说这是一个非常简单的问题,所以我大喊“救命!” 有人!
我的 items.py 文件也如下所示,据我所知,我认为这两个文件 items.py 和 toscrape-xpath.py 是唯一有效的文件,因为我对这一切都很陌生。
# -*- coding: utf-8 -*-`enter code here`
# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html
import scrapy
class QuotesbotItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
pass
class MyItems(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
tinfo = scrapy.Field()
pass
非常感谢您提供的所有帮助
# -*- coding: utf-8 -*-
import scrapy
from quotesbot.items import MyItems
from scrapy import Request
class ToScrapeSpiderXPath(scrapy.Spider):
name = 'toscrape-xpath'
start_urls = [
'http://quotes.toscrape.com/',
]
def parse(self, response):
item = MyItems()
for quote in response.xpath('//div[@class="quote"]'):
item =
{'tinfo':quote.xpath('./span[@class="text"]/text()').extract_first()}
**yield response.follow**('http://quotes.toscrape.com', self.parse_2,
meta={'item':item})
def parse_2(self, response):
print "almost there"
item = response.meta['item']
yield item
解决方案
您的蜘蛛逻辑非常混乱:
def parse(self, response):
for quote in response.xpath('//div[@class="quote"]'):
yield Request("http://quotes.toscrape.com/",
callback=self.parse2, meta={'item':item})
对于您在同一网页上找到的每个报价quotes.toscrape.com
安排另一个请求?发生的情况是这些新的计划请求被scrapys重复请求过滤器过滤掉了。
也许您应该在此处生成该项目:
def parse(self, response):
for quote in response.xpath('//div[@class="quote"]'):
item = MyItems()
item['tinfo'] = quote.xpath('./span[@class="text"]/text()').extract_first()
yield item
推荐阅读
- windows - 无法在 Chrome 远程桌面上输入(键入)PIN
- reactjs - 如何使用 React-Redux-Firebase 从 Firestore 子集合中检索和加载数据?
- python - netloc 是什么意思?
- swift - 使 arc4random,更随机?
- vue.js - path-to-regexp 查找匹配路由的正则表达式
- html - 在选择 - 选项 Thymeleaf 和 Spring Boot 中获取选定的值
- here-api - 在地理编码和计算路线中支持机场代码
- python - mycursor.executemany UPDATE 未按预期工作
- java - Leetcode 833:字符串替换取决于索引编号
- vuejs2 - 在 vue.js 中设置 el-input 和 el-select 行数组元素的焦点?