strong text基于我搜索的示例的代码似乎没有按预期运行,因此我决定使用在github上找到的工作模型:https://github.com/scrapy/quotesbot/blob/master/quotesbot/spiders/toscrape-xpath.py 然后我稍微修改了一下,以展示我遇到了什么。下面的代码工作得很好,但我的最终目标是将第一个“parse”中的数据传递给第二个“parse2”函数,这样我就可以合并来自两个不同页面的数据。但是现在我想从一个非常简单的开始,这样我就可以跟踪正在发生的事情,因此下面的代码被严重剥离了。你知道吗
# -*- 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”根本就没有读过。我一直在玩缩进和括号,以为我错过了一些简单的东西,但没有太大的成功。我看了很多例子,看看我是否能理解什么可能是问题,但我仍然无法使它工作。我相信这对那些大师来说是一个非常简单的问题,所以我大叫“救命!”某人!你知道吗
也是我的项目.py文件如下所示,我认为这两个文件项目.py还有托斯卡普-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
你的蜘蛛逻辑很混乱:
对于您在
quotes.toscrape.com
上找到的每一个报价,您是否安排另一个请求到同一个网页? 所发生的是这些新的计划请求被scrapys duplicate request filter过滤掉。你知道吗也许你应该把物品放在这里:
为了说明当前爬虫程序什么都不做的原因,请参见此图:
相关问题 更多 >
编程相关推荐