抓取嵌套URL

2024-05-14 08:25:20 发布

您现在位置:Python中文网/ 问答频道 /正文

导言

由于我必须更深入地研究爬行,我面临着下一个问题:爬行嵌套页面,如:https://www.karton.eu/Faltkartons

我的爬虫程序必须从这个页面开始,转到https://www.karton.eu/Einwellige-Kartonagen并访问这个类别中列出的每个产品

对于每个类别中包含的每个产品,“Faltkartons”的每个子类别都应该这样做

已编辑

我的代码现在看起来像这样:

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

在我的脑海中,它从开始的url开始,然后我访问https://www.karton.eu/Einwellige-Kartonagen,寻找链接并跟随它们到达 https://www.karton.eu/einwellig-ab-100-mm。在该页面上,它检查卡片上的一些信息,并按照特定产品页面的链接获取最后的项目

我的方法的哪部分是错误的? 我应该把我的课从“痒痒的蜘蛛”改成“爬行的蜘蛛”吗?或者这仅仅是我想设定一些规则时才需要的

仍然有可能,我对标题、sku等的XPath可能是错误的,但首先,我只想构建我的基础,对这些嵌套页面进行爬网

我的控制台输出:

console output

最后,我设法浏览了所有这些页面,但不知何故,我的.csv文件仍然是空的


Tags: texthttpsselfdivurlgetparseresponse
1条回答
网友
1楼 · 发布于 2024-05-14 08:25:20

根据您提供的评论,问题始于您跳过链中的请求

您的start_urls将请求此页面:https://www.karton.eu/Faltkartons 页面将由parse方法解析,并产生从https://www.karton.eu/Karton-weisshttps://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时,将从中找到9个新链接

最后,您需要一个解析方法来刮取这些页面中的项目。由于有多个项,我建议您在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方法中更改此

    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在该方法中不存在

相关问题 更多 >

    热门问题