我有一个问题,我想解析一个网站并从中抓取每篇文章的链接,但问题是Scrapy不抓取所有链接并随机抓取其中一些链接。用Scrapy解析文档
import scrapy
from tutorial.items import GouvItem
class GouvSpider(scrapy.Spider):
name = "gouv"
allowed_domains = ["legifrance.gouv.fr"]
start_urls = [
"http://www.legifrance.gouv.fr/affichCode.do?cidTexte=LEGITEXT000006069577&dateTexte=20160128"
]
def parse(self, response):
for href in response.xpath('//span/a/@href'):
url = response.urljoin(href.extract())
yield scrapy.Request(url, callback=self.parse_article)
def parse_article(self, response):
for art in response.xpath("//div[@class='corpsArt']"):
item = GouvItem()
item['article'] = art.xpath('p/text()').extract()
yield item
#And this is the GouvItem :
import scrapy
class GouvItem(scrapy.Item):
title1 = scrapy.Field()
title2 = scrapy.Field()
title3 = scrapy.Field()
title4 = scrapy.Field()
title5 = scrapy.Field()
title6 = scrapy.Field()
link = scrapy.Field()
article = scrapy.Field()
的问题是,法律的每篇文章应该有且只有一次。在网站上,每篇文章只出现一次。
非常感谢!
请编辑你的帖子并在这里粘贴你的代码以便人们可以将其复制粘贴到他们的编辑器中 –
包括'GouvItem'的定义,太好了 –
......我只是意识到如果我执行相同的脚本两次,两个结果都不一样......我不明白... –