CrawlSpider爬取聚美优品之翻页(MongoDB)

自从学习了上个案例(CrawlSpider爬虫之爬取17k小说网列表详情及章节并放在一起(CrawlSpider翻页、MongoDB)-CSDN博客),做这个就简单多了,视频教程里也很简单,毕竟是入门CrawlSpider的实战小demo。这个视频教程真的做的很贴心。

聚美优品上打不开兰蔻品牌的链接啊,显示404啊。是不是爬崩溃了😄……

选择雅诗兰黛这个品牌,而且需要在其他页面,才能选择下拉菜单,看把人家聚美优品折腾的,首页都不敢放下拉菜单了~~~~

废话不多说,我忒忙……上代码

app.py

from typing import Iterable

import scrapy
from scrapy import Request
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from ..items import jumei_product


class AppSpider(CrawlSpider):
    name = "app"
    start_urls = [
        "http://search.jumei.com/?filter=0-11-1&search=%E9%9B%85%E8%AF%97%E5%85%B0%E9%BB%9B&bid=4&site=sh"]

    rules = (
        Rule(LinkExtractor(allow=r"http://item.jumeiglobal.com/(.+).html",
                           restrict_xpaths=('//div[@class="s_l_pic"]/a')),
             callback="parse_detail", follow=False, process_links="process_detail"),
    )

    def start_requests(self) -> Iterable[Request]:
        max_page = 4
        for i in range(1, max_page):
            url = "http://search.jumei.com/?filter=0-11-" + str(
                i) + "&search=%E9%9B%85%E8%AF%97%E5%85%B0%E9%BB%9B&bid=4&site=sh"
            yield Request(url)

    def process_detail(self, links):
        for index, link in enumerate(links):
            # 列表页,每页选5个商品
            if index < 5:
                yield link
            else:
                return

    def parse_detail(self, response):
        # 商品详情数据信息
        title = response.xpath('//div[@class="deal_con_content"]//tr[1]/td[2]/span/text()').get()
        category = response.xpath('//div[@class="deal_con_content"]//tr[4]/td[2]/span/text()').get()
        address = response.xpath('//div[@class="deal_con_content"]//tr[6]/td[2]/span/text()').get()
        expired = response.xpath('//div[@class="deal_con_content"]//tr[8]/td[2]/span/text()').get()

        item = jumei_product()
        item["title"] = title
        item["category"] = category
        item["address"] = address
        item["expired"] = expired
        yield item


列表页选择5个商品,选择循环3个列表页面。

items.py

import scrapy


class jumei_product(scrapy.Item):
    title = scrapy.Field()
    category = scrapy.Field()
    address = scrapy.Field()
    expired = scrapy.Field()

数据库实体类pipelines.py

import pymongo


class Scrapy02Pipeline:
    def __init__(self):
        print("-" * 10, "开始", "-" * 10)
        self.res = None
        self.client = pymongo.MongoClient("mongodb://localhost:27017")
        self.db = self.client["jumei"]
        self.collection = self.db["landai"]
        self.collection.delete_many({})

    def process_item(self, item, spider):
        self.res = self.collection.insert_one(dict(item))
        # print(self.res.inserted_id)
        return item

    def __del__(self):
        print("-" * 10, "结束", "-" * 10)

是不是感觉有手就行了?😄

学无止境,学到后期,不仅仅是有手就行,要做到无手自行才行吧……

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

andux

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值