scrapy爬取华为商城所有商品信息--科技快人一步

华为商城 https://www.vmall.com/index.html

目标:华为商城下的商品信息
  • 按主页的左边手机,笔记本&平板,智能穿戴……分类
  • 每一个分类下的小分类
    • 商品标题
    • 商品价格
  • 规格参数
    • 主要参数
    • 主体
    • ……
    • 商品编码
  • 写入excel
  • 设置好excel数据表,分析数据

代码如下(scrapy):

# -*- coding: utf-8 -*-
import os
import re
import urllib.request
from copy import deepcopy

import scrapy
import xlrd
import xlwt
from ..items import HuaweiItem


class HuaWei(scrapy.Spider):
    name = 'huawei'
    allowed_domains = ['vmall.com', 'vmallres.com']
    start_urls = ['http://vmall.com/']

    def parse(self, response):
        self.new_xls()
        # 主页
        print("分割线-----------------------主页------------------------分割线")
        classify_list_A = response.xpath('//div[@id="category-block"]/div/ol/li')
        print("大分类长度:", len(classify_list_A))
        for i in classify_list_A:
            # print("现在位置:", classify_list_A)
            item = HuaweiItem()
            item['classify_A'] = i.xpath('.//input[2]/@value').extract_first()
            classify_list = i.xpath('.//div[2]//li[not(@class="subcate-btn")]')
            # classify_list = i.xpath('.//div[2]//li[last()]')
            for i in classify_list:
                item['classify_B'] = i.xpath('.//input[1]/@value').extract_first()
                href = "https://www.vmall.com" + str(i.xpath('.//a/@href').extract_first()) + '-1-3-0'
                # print("href:", href)
                yield scrapy.Request(
                    href,
                    callback=self.parse_A,
                    meta={
   
   "item": deepcopy(item)}
                )
        rb = xlrd.open_workbook('华为商城.xls')
        # 通过sheet_by_index()获取的sheet
        rs = rb.sheet_by_index(0)
        print("已爬取的商品数量:", rs.nrows - 1)

    def parse_A(self, response):
        # 中间页
        print("分割线-----------------------中间页------------------------分割线")
        li_list = response.xpath('//div[@class="layout"]/div[@class="channel-list"]/div[@class="pro-list clearfix"]/ul/li')
        if li_list:
            print("正在爬取页面链接:", response.request.url)
            print("此页面商品数量:", len(li_list))
            for i in li_list:
                item = response.meta["item"]
                rb = xlrd.open_workbook('华为商城.xls')
                # 通过sheet_by_index()获取的sheet
                rs = rb.sheet_by_index(0)
                cods = rs.col_values(0, start_rowx=0, end_rowx=None)
                item[</
爬取网站上的商品信息通常需要使用网络爬虫技术,针对畅购华为手表商品信息爬取可以分为以下几个步骤: 1. **目标定位**:首先确定你要抓取的具体华为手表商品页面URL,比如畅购官网的华为手表分类页或单个产品详情页。 2. **分析网页结构**:查看HTML源码,理解商品信息是如何通过CSS选择器或XPath表达式组织的。商品标题、价格、图片链接等元素通常会有特定的标识。 3. **编写爬虫脚本**:使用Python的requests库获取网页内容,如BeautifulSoup或Scrapy框架可以帮助解析HTML,提取出所需的数据。例如,你可以找到如下代码片段: ```python import requests from bs4 import BeautifulSoup url = "https://chaogou.com/huawei-watch" # 示例地址,需替换为你找到的商品列表URL response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') # 使用CSS选择器或XPath找出商品信息 title_tags = soup.select('.product-title') # 假设标题在一个class为'product-title'的元素中 prices = soup.find_all('span', class_='price') # 类似地找价格标签 for title, price in zip(title_tags, prices): print(f"标题:{title.text.strip()}\n价格:{price.text.strip()}") ``` 4. **处理反爬策略**:网站可能会有防止爬虫的机制,如验证码、IP限制、User-Agent检测等。你需要根据实际情况设置代理、设置合理的请求间隔,并可能使用cookies或者模拟登录来绕过一些限制。 5. **数据存储**:将抓取到的信息保存到本地文件、数据库或直接导入数据分析工具,以便后续处理。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值