以下是我的蜘蛛的代碼:在Scrapy中,如何將一個類中生成的url傳遞給腳本中的下一個類?
import scrapy
class ProductMainPageSpider(scrapy.Spider):
name = 'ProductMainPageSpider'
start_urls = ['http://domain.com/main-product-page']
def parse(self, response):
for product in response.css('article.isotopeItem'):
yield {
'title': product.css('h3 a::text').extract_first().encode("utf-8"),
'category': product.css('h6 a::text').extract_first(),
'img': product.css('figure a img::attr("src")').extract_first(),
'url': product.css('h3 a::attr("href")').extract_first()
}
class ProductSecondaryPageSpider(scrapy.Spider):
name = 'ProductSecondaryPageSpider'
start_urls = """ URLS IN product['url'] FROM PREVIOUS CLASS """
def parse(self, response):
for product in response.css('article.isotopeItem'):
yield {
'title': product.css('h3 a::text').extract_first().encode("utf-8"),
'thumbnail': product.css('figure a img::attr("src")').extract_first(),
'short_description': product.css('div.summary').extract_first(),
'description': product.css('div.description').extract_first(),
'gallery_images': product.css('figure a img.gallery-item ::attr("src")').extract_first()
}
第一類/部件正常工作,如果我刪除第二類/一部分。它使用它中請求的項目正確地生成我的json文件。不過,我需要抓取的網站是一個雙人網站。它有一個產品存檔頁面,可將產品顯示爲縮略圖,標題和類別(並且此信息不在下一頁中)。然後,如果您點擊其中一個縮略圖或標題,就會將其發送到產品上具有特定信息的單個產品頁面。
有很多產品,所以我想將產品['url']中的url作爲「start_urls」列表管道(yield?)到第二個類。但我根本不知道該怎麼做。我的知識還遠遠不夠,甚至不知道我錯過了什麼或錯在哪裏,以便我可以找到解決方案。
在第20行查看我想要做什麼。
我不t取得「MyItem()」部分來自哪裏。那是什麼?實際上你的答案比以前更讓我困惑。 –
@AdrianoBatista dude,'MyItem'只是一個佔位符名稱,不管你的'scrapy.Item'類是什麼,你當然可以用一個簡單的字典替換它,但通常你想要堅持'scrapy.Item'。如果你對這個答案感到困惑,你應該讓教程去熟悉蜘蛛,項目和請求的基本概念:https://doc.scrapy.org/en/latest/intro/tutorial.html – Granitosaurus