我有一個問題,我想解析一個網站並從中抓取每篇文章的鏈接,但問題是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'的定義,太好了 –
......我只是意識到如果我執行相同的腳本兩次,兩個結果都不一樣......我不明白... –