Introduction
当我必须更深入地爬行时,我面临着我的下一个问题:爬行嵌套的页面,如:https://www.karton.eu/Faltkartons
我的爬虫必须从这个页面开始,进入https://www.karton.eu/Einwellige-Kartonagen并访问这个类别中列出的每一个产品。
它应该对每个类别中的每一个产品进行“Faltkarton”的每一次分类。
编辑的
我的代码现在看起来如下:
import scrapy
from ..items import KartonageItem
class KartonSpider(scrapy.Spider):
name = "kartons12"
allow_domains = ['karton.eu']
start_urls = [
'https://www.karton.eu/Faltkartons'
]
custom_settings = {'FEED_EXPORT_FIELDS': ['SKU', 'Title', 'Link', 'Price', 'Delivery_Status', 'Weight', 'QTY', 'Volume'] }
def parse(self, response):
url = response.xpath('//div[@class="cat-thumbnails"]')
for a in url:
link = a.xpath('a/@href')
yield response.follow(url=link.get(), callback=self.parse_category_cartons)
def parse_category_cartons(self, response):
url2 = response.xpath('//div[@class="cat-thumbnails"]')
for a in url2:
link = a.xpath('a/@href')
yield response.follow(url=link.get(), callback=self.parse_target_page)
def parse_target_page(self, response):
card = response.xpath('//div[@class="text-center articelbox"]')
for a in card:
items = KartonageItem()
link = a.xpath('a/@href')
items ['SKU'] = a.xpath('.//div[@class="delivery-status"]/small/text()').get()
items ['Title'] = a.xpath('.//h5[@class="title"]/a/text()').get()
items ['Link'] = a.xpath('.//h5[@class="text-center artikelbox"]/a/@href').extract()
items ['Price'] = a.xpath('.//strong[@class="price-ger price text-nowrap"]/span/text()').get()
items ['Delivery_Status'] = a.xpath('.//div[@class="signal_image status-2"]/small/text()').get()
yield response.follow(url=link.get(),callback=self.parse_item, meta={'items':items})
def parse_item(self,response):
table = response.xpath('//div[@class="product-info-inner"]')
items = KartonageItem()
items = response.meta['items']
items['Weight'] = a.xpath('.//span[@class="staffelpreise-small"]/text()').get()
items['Volume'] = a.xpath('.//td[@class="icon_contenct"][7]/text()').get()
yield items在我的脑海中,它从start_url开始,然后我访问https://www.karton.eu/Einwellige-Kartonagen,查找链接并跟踪它们到https://www.karton.eu/einwellig-ab-100-mm.On,该页面检查卡片上的一些信息,然后跟踪到特定产品页面的链接,以获得最后的项目。
我的方法哪一部分是错误的?我应该把我的课从"scrapy.Spider“改为"crawl.spider”吗?或者只有当我想制定一些规则的时候才需要这样做?
仍然有可能,我的标题、sku等的xpath可能是错误的,但在一开始,我只想构建我的基础,来爬行这些嵌套的页面。
我的控制台输出:

最后,我设法浏览了所有这些页面,但不知怎么的,我的..csv文件仍然是空的。
发布于 2020-07-30 13:33:42
根据您提供的评论,问题从您跳过链中的请求开始。
您的start_urls将请求此页面:https://www.karton.eu/Faltkartons页面将由parse方法解析,并生成从https://www.karton.eu/Karton-weiss到https://www.karton.eu/Einwellige-Kartonagen的新请求
这些页面将在parse_item方法中进行解析,但它们不是您想要的最终页面。您需要在卡片之间进行解析并生成新请求,如下所示:
for url in response.xpath('//div[@class="cat-thumbnails"]/div/a/@href')
yield scrapy.Request(response.urljoin(url.get()), callback=self.new_parsing_method)这里的示例,当解析https://www.karton.eu/Zweiwellige-Kartons时,将从
最后,您需要一个解析方法来刮取这些页面中的项。由于有多个项目,我建议您在一个for循环中运行它们。(您需要正确的xpath来刮取数据。)
编辑:
重新编辑,就像我现在观察到的页面结构,并看到我的代码是基于错误的假设。问题是,有些页面没有子类别页面,而另一些页面则有。
页面结构:
ROOT: www.karton.eu/Faltkartons
|_ Einwellige Kartons
|_ Subcategory: Kartons ab 100 mm Länge
|_ Item List (www.karton.eu/einwellig-ab-100-mm)
|_ Item Detail (www.karton.eu/113x113x100-mm-einwellige-Kartons)
...
|_ Subcategory: Kartons ab 1000 mm Länge
|_ ...
|_ Zweiwellige Kartons #Same as above
|_ Lange Kartons #Same as above
|_ quadratische Kartons #There is no subcategory
|_ Item List (www.karton.eu/quadratische-Kartons)
|_ Item Detail (www.karton.eu/113x113x100-mm-einwellige-Kartons)
|_ Kartons Höhenvariabel #There is no subcategory
|_ Kartons weiß #There is no subcategory下面的代码将从带有子类别的页面中抓取项目,因为我认为这就是您想要的。不管怎样,我留下了一个print语句来显示由于没有子类别页面而被跳过的页面。万一你以后想把它们包括进去。
import scrapy
from ..items import KartonageItem
class KartonSpider(scrapy.Spider):
name = "kartons12"
allow_domains = ['karton.eu']
start_urls = [
'https://www.karton.eu/Faltkartons'
]
custom_settings = {'FEED_EXPORT_FIELDS': ['SKU', 'Title', 'Link', 'Price', 'Delivery_Status', 'Weight', 'QTY', 'Volume'] }
def parse(self, response):
url = response.xpath('//div[@class="cat-thumbnails"]')
for a in url:
link = a.xpath('a/@href')
yield response.follow(url=link.get(), callback=self.parse_category_cartons)
def parse_category_cartons(self, response):
url2 = response.xpath('//div[@class="cat-thumbnails"]')
if not url2:
print('Empty url2:', response.url)
for a in url2:
link = a.xpath('a/@href')
yield response.follow(url=link.get(), callback=self.parse_target_page)
def parse_target_page(self, response):
card = response.xpath('//div[@class="text-center artikelbox"]')
for a in card:
items = KartonageItem()
link = a.xpath('a/@href')
items ['SKU'] = a.xpath('.//div[@class="delivery-status"]/small/text()').get()
items ['Title'] = a.xpath('.//h5[@class="title"]/a/text()').get()
items ['Link'] = a.xpath('.//h5[@class="text-center artikelbox"]/a/@href').extract()
items ['Price'] = a.xpath('.//strong[@class="price-ger price text-nowrap"]/span/text()').get()
items ['Delivery_Status'] = a.xpath('.//div[@class="signal_image status-2"]/small/text()').get()
yield response.follow(url=link.get(),callback=self.parse_item, meta={'items':items})
def parse_item(self,response):
table = response.xpath('//div[@class="product-info-inner"]')
#items = KartonageItem() # You don't need this here, as the line bellow you are overwriting the variable.
items = response.meta['items']
items['Weight'] = response.xpath('.//span[@class="staffelpreise-small"]/text()').get()
items['Volume'] = response.xpath('.//td[@class="icon_contenct"][7]/text()').get()
yield items备注
改变了这一点:
card = response.xpath('//div[@class="text-center articelbox"]')对此:(K而不是C)
card = response.xpath('//div[@class="text-center artikelbox"]')注释这一点,因为meta中的项已经是一个KartonageItem。(你可以移除它)
def parse_item(self,response):
table = response.xpath('//div[@class="product-info-inner"]')
#items = KartonageItem()
items = response.meta['items']在 parse_items 方法中更改此parse_items
items['Weight'] = a.xpath('.//span[@class="staffelpreise-small"]/text()').get()
items['Volume'] = a.xpath('.//td[@class="icon_contenct"][7]/text()').get()对此:
items['Weight'] = response.xpath('.//span[@class="staffelpreise-small"]/text()').get()
items['Volume'] = response.xpath('.//td[@class="icon_contenct"][7]/text()').get()因为a不存在于该方法中。
https://stackoverflow.com/questions/63171747
复制相似问题