繁体   English   中英

爬网爬网类别链接,直到产品页面

[英]Scrapy crawl category links till product page

您好,我正在尝试抓取一个电子商务网站,该网站通过滚动加载数据并加载更多按钮,我遵循此步骤如何通过无限滚动来抓取网站? 链接,但是当我尝试执行代码时,它关闭了没有产品的蜘蛛程序,可能是结构发生了变化,我希望获得一些帮助以帮助我起步,我对网络爬虫非常陌生。

编辑问题,确定,我正在抓取此网站[link] http://www.jabong.com/women/ ,其中包含子类别,我正在尝试抓取我在上面的代码中尝试过的所有子类别产品,但是在进行了一些研究后并未奏效我创建了一个有效的代码,但不满足我的目标。到目前为止,我已经尝试过了

` import scrapy
#from scrapy.exceptions import CloseSpider

from scrapy.spiders import Spider
#from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.http import Request
from koovs.items import product
#from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import Selector    

class propubSpider(scrapy.Spider):
name = 'koovs'
allowed_domains = ['jabong.com']
max_pages = 40

def start_requests(self):
    for i in range(self.max_pages):
         yield scrapy.Request('http://www.jabong.com/women/clothing/tops-tees-shirts/tops/?page=%d' % i, callback=self.parse)

def parse(self, response):
    for sel in response.xpath('//*[@id="catalog-product"]/section[2]'):
        item = product()
        item['price'] = sel.xpath('//*[@class="price"]/span[2]/text()').extract()
        item['image'] = sel.xpath('//*[@class="primary-image thumb loaded"]/img').extract()
        item['title'] = sel.xpath('//*[@data-original-href]/@href').extract()
     `

上面的代码适用于一个类别,并且如果我指定页面数,则上面的网站针对给定的类别有很多产品,并且我不知道它们驻留在多少页面中,因此我决定使用抓取蜘蛛浏览所有内容类别和产品页面以及获取数据,但是我非常陌生,对任何帮助都将不胜感激

您需要了解的第一件事是网站的DOM结构经常变化。 因此,过去编写的刮板现在可能会或可能不适合您。

因此,抓取网站时最好的方法是找到一个隐藏的api或一个隐藏的url,只有在您对网站的网络流量进行分析后才能看到它们。 这不仅为您提供了可靠的抓取解决方案,而且还节省了带宽,这在进行广泛抓取时非常重要,因为在大多数情况下,您无需下载整个页面。

让我们以您正在爬网的网站为例,以使其更加清晰。 当您访问此页面时,您会看到Show More Product的按钮。 转到浏览器的开发人员工具,然后选择网络分析仪。 当您单击按钮时,您将看到浏览器向此链接发送GET请求。 检查响应,您将在第一页中看到所有产品的列表。 现在,当您分析此链接时,您可以看到它具有一个参数page=1 将其更改为page=2 ,您将在第二页中看到所有产品的列表。

现在继续写蜘蛛。 它将类似于:

import scrapy
from scrapy.exceptions import CloseSpider
from scrapy.spider import BaseSpider
from scrapy.http import Request
from jabong.items import product

class aqaqspider(BaseSpider):
    name = "jabong"
    allowed_domains = ["jabong.com"]
    start_urls = [
    "http://www.jabong.com/women/clothing/tops-tees-shirts/tops/?page=1&limit=52&sortField=popularity&sortBy=desc",
]
    page = 1

    def parse(self, response):
        products = response.xpath('//*[@id="catalog-product"]/section[2]/div')
        if not products:
            raise CloseSpider("No more products!")

        for p in products:
            item = product()
            item['price'] = p.xpath('a/div/div[2]/span[@class="standard-price"]/text()').extract()
            item['title'] = p.xpath('a/div/div[1]/text()').extract()
            if item['title']:
                yield item

        self.page += 1
        yield Request(url="http://www.jabong.com/women/clothing/tops-tees-shirts/tops/?page=%d&limit=52&sortField=popularity&sortBy=desc" % self.page,
                  callback=self.parse, 
                  dont_filter=True)  

注意:此示例仅用于教育目的。 在抓取/抓取/存储网站上的任何数据之前,请参阅网站的Terms and Conditions/Privacy Policy/Robots.txt

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM