国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【Python爬蟲+數(shù)據(jù)分析】采集電商平臺(tái)數(shù)據(jù)信息,并做可視化演示

這篇具有很好參考價(jià)值的文章主要介紹了【Python爬蟲+數(shù)據(jù)分析】采集電商平臺(tái)數(shù)據(jù)信息,并做可視化演示。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。


前言

隨著電商平臺(tái)的興起,越來越多的人開始在網(wǎng)上購物。而對于電商平臺(tái)來說,商品信息、價(jià)格、評(píng)論等數(shù)據(jù)是非常重要的。因此,抓取電商平臺(tái)的商品信息、價(jià)格、評(píng)論等數(shù)據(jù)成為了一項(xiàng)非常有價(jià)值的工作。本文將介紹如何使用Python編寫爬蟲程序,抓取電商平臺(tái)的商品信息、價(jià)格、評(píng)論等數(shù)據(jù)。

給大家準(zhǔn)備了一些Python相關(guān)的資料都可拿走

【Python爬蟲+數(shù)據(jù)分析】采集電商平臺(tái)數(shù)據(jù)信息,并做可視化演示

一、準(zhǔn)備工作

在開始編寫爬蟲程序之前,我們需要準(zhǔn)備一些工具和環(huán)境。

Python3.8
PyCharm

二、分析目標(biāo)網(wǎng)站

在開始編寫爬蟲程序之前,我們需要先分析目標(biāo)網(wǎng)站的結(jié)構(gòu)和數(shù)據(jù)。在本文中,我們選擇抓取京東商城的商品信息、價(jià)格、評(píng)論等數(shù)據(jù)。

1.商品信息

  1. 商城的商品信息包括商品名稱、商品編號(hào)、商品分類、商品品牌、商品型號(hào)、商品規(guī)格、商品產(chǎn)地、商品重量、商品包裝等信息。這些信息可以在商品詳情頁面中找到。

  2. 價(jià)格
    商城的商品價(jià)格包括商品原價(jià)、商品促銷價(jià)、商品折扣等信息。這些信息可以在商品詳情頁面中找到。

  3. 評(píng)論
    京東商城的商品評(píng)論包括用戶評(píng)價(jià)、用戶曬圖、用戶追評(píng)等信息。這些信息可以在商品詳情頁面中找到。

三、編寫爬蟲程序

在分析目標(biāo)網(wǎng)站的結(jié)構(gòu)和數(shù)據(jù)之后,我們可以開始編寫爬蟲程序了。在本文中,我們使用Scrapy框架編寫爬蟲程序,將抓取到的數(shù)據(jù)保存到MySQL數(shù)據(jù)庫中。

  1. 創(chuàng)建Scrapy項(xiàng)目

首先,我們需要?jiǎng)?chuàng)建一個(gè)Scrapy項(xiàng)目。在命令行中輸入以下命令:

scrapy startproject jingdong

這將創(chuàng)建一個(gè)名為jingdong的Scrapy項(xiàng)目。

  1. 創(chuàng)建爬蟲

接下來,我們需要?jiǎng)?chuàng)建一個(gè)爬蟲。在命令行中輸入以下命令:

scrapy genspider jingdong_spider jd.com

這將創(chuàng)建一個(gè)名為jingdong_spider的爬蟲,爬取的網(wǎng)站為jd.com。

  1. 編寫爬蟲代碼

在創(chuàng)建完爬蟲之后,我們需要編寫爬蟲代碼。在Scrapy框架中,爬蟲代碼主要包括以下幾個(gè)部分:

(1)定義Item

Item是Scrapy框架中的一個(gè)概念,它用于定義要抓取的數(shù)據(jù)結(jié)構(gòu)。在本文中,我們需要定義一個(gè)Item,用于保存商品信息、價(jià)格、評(píng)論等數(shù)據(jù)。在項(xiàng)目的items.py文件中,添加以下代碼:

import scrapy

class JingdongItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    name = scrapy.Field()
    sku = scrapy.Field()
    category = scrapy.Field()
    brand = scrapy.Field()
    model = scrapy.Field()
    spec = scrapy.Field()
    origin = scrapy.Field()
    weight = scrapy.Field()
    package = scrapy.Field()
    price = scrapy.Field()
    promotion_price = scrapy.Field()
    discount = scrapy.Field()
    comment = scrapy.Field()
    image_urls = scrapy.Field()
    images = scrapy.Field()

這里定義了一個(gè)名為JingdongItem的Item,包括商品名稱、商品編號(hào)、商品分類、商品品牌、商品型號(hào)、商品規(guī)格、商品產(chǎn)地、商品重量、商品包裝、商品價(jià)格、商品促銷價(jià)、商品折扣、商品評(píng)論、商品圖片等字段。

(2)編寫爬蟲代碼
在項(xiàng)目的spiders目錄下,打開jingdong_spider.py文件,添加以下代碼:

import scrapy
from jingdong.items import JingdongItem

class JingdongSpider(scrapy.Spider):
    name = 'jingdong'
    allowed_domains = ['jd.com']
    start_urls = ['https://www.jd.com/']

    def parse(self, response):
        # 獲取所有分類鏈接
        category_links = response.xpath('//div[@class="category-item"]/div[@class="item-list"]/ul/li/a/@href')
        for link in category_links:
            yield scrapy.Request(link.extract(), callback=self.parse_category)

    def parse_category(self, response):
        # 獲取所有商品鏈接
        product_links = response.xpath('//div[@class="gl-i-wrap"]/div[@class="p-img"]/a/@href')
        for link in product_links:
            yield scrapy.Request(link.extract(), callback=self.parse_product)

        # 獲取下一頁鏈接
        next_page_link = response.xpath('//a[@class="pn-next"]/@href')
        if next_page_link:
            yield scrapy.Request(next_page_link.extract_first(), callback=self.parse_category)

    def parse_product(self, response):
        item = JingdongItem()

        # 獲取商品名稱
        item['name'] = response.xpath('//div[@class="sku-name"]/text()')[0].extract()

        # 獲取商品編號(hào)
        item['sku'] = response.xpath('//div[@class="itemInfo-wrap"]/div[@class="clearfix"]/div[@class="sku"]/div[@class="item"]/div[@class="name"]/text()')[0].extract()

        # 獲取商品分類
        category_list = response.xpath('//div[@class="breadcrumb"]/a/text()')
        item['category'] = '>'.join(category_list.extract())

        # 獲取商品品牌
        item['brand'] = response.xpath('//div[@class="itemInfo-wrap"]/div[@class="clearfix"]/div[@class="sku-name"]/a/@title')[0].extract()

        # 獲取商品型號(hào)
        item['model'] = response.xpath('//div[@class="Ptable"]/div[@class="Ptable-item"]/dl/dt/text()')[0].extract()

        # 獲取商品規(guī)格
        spec_list = response.xpath('//div[@class="Ptable"]/div[@class="Ptable-item"]/dl/dd/ul/li/text()')
        item['spec'] = ','.join(spec_list.extract())

        # 獲取商品產(chǎn)地
        item['origin'] = response.xpath('//div[@class="Ptable"]/div[@class="Ptable-item"]/dl/dd/text()')[0].extract()

        # 獲取商品重量
        item['weight'] = response.xpath('//div[@class="Ptable"]/div[@class="Ptable-item"]/dl/dd/text()')[1].extract()

        # 獲取商品包裝
        item['package'] = response.xpath('//div[@class="Ptable"]/div[@class="Ptable-item"]/dl/dd/text()')[2].extract()

        # 獲取商品價(jià)格
        price_list = response.xpath('//div[@class="summary-price-wrap"]/div[@class="summary-price J-summary-price"]/div[@class="dd"]/span/text()')
        item['price'] = price_list[0].extract()
        item['promotion_price'] = price_list[1].extract() if len(price_list) > 1 else ''
        item['discount'] = response.xpath('//div[@class="summary-price-wrap"]/div[@class="summary-price J-summary-price"]/div[@class="dd"]/div[@class="promo"]/span/text()')[0].extract()

        # 獲取商品評(píng)論
        comment_list = response.xpath('//div[@class="comment-item"]')
        comment_text_list = []
        for comment in comment_list:
            comment_text = comment.xpath('div[@class="comment-column J-comment-column"]/div[@class="comment-con"]/div[@class="comment-con-top"]/div[@class="comment-con-txt"]/text()').extract_first()
            if comment_text:
                comment_text_list.append(comment_text.strip())
        item['comment'] = '\n'.join(comment_text_list)

        # 獲取商品圖片
        item['image_urls'] = response.xpath('//div[@class="spec-items"]/ul/li/img/@src')
        item['images'] = []

        yield item

這里定義了一個(gè)名為JingdongSpider的爬蟲,首先獲取所有分類鏈接,然后依次訪問每個(gè)分類頁面,獲取所有商品鏈接,然后依次訪問每個(gè)商品頁面,抓取商品信息、價(jià)格、評(píng)論等數(shù)據(jù),并保存到Item中。

(3)配置數(shù)據(jù)庫

在項(xiàng)目的settings.py文件中,添加以下代碼:

ITEM_PIPELINES = {
    'jingdong.pipelines.JingdongPipeline': 300,
}

MYSQL_HOST = 'localhost'
MYSQL_PORT = 3306
MYSQL_USER = 'root'
MYSQL_PASSWORD = '123456'
MYSQL_DBNAME = 'jingdong'

這里定義了一個(gè)名為JingdongPipeline的管道,用于將抓取到的數(shù)據(jù)保存到MySQL數(shù)據(jù)庫中。同時(shí),配置了MySQL數(shù)據(jù)庫的連接信息。

(4)編寫管道代碼

在項(xiàng)目的pipelines.py文件中,添加以下代碼:

import pymysql
from scrapy.exceptions import DropItem
from scrapy.pipelines.images import ImagesPipeline
from jingdong.items import JingdongItem

class JingdongPipeline(object):
    def __init__(self, host, port, user, password, dbname):
        self.host = host
        self.port = port
        self.user = user
        self.password = password
        self.dbname = dbname

    @classmethod
    def from_crawler(cls, crawler):
        return cls(
            host=crawler.settings.get('MYSQL_HOST'),
            port=crawler.settings.get('MYSQL_PORT'),
            user=crawler.settings.get('MYSQL_USER'),
            password=crawler.settings.get('MYSQL_PASSWORD'),
            dbname=crawler.settings.get('MYSQL_DBNAME')
        )

    def open_spider(self, spider):
        self.conn = pymysql.connect(host=self.host, port=self.port, user=self.user, password=self.password, db=self.dbname, charset='utf8')
        self.cursor = self.conn.cursor()

    def close_spider(self, spider):
        self.conn.close()

    def process_item(self, item, spider):
        if not isinstance(item, JingdongItem):
            return item

        # 保存商品信息
        sql = 'INSERT INTO product(name, sku, category, brand, model, spec, origin, weight, package, price, promotion_price, discount, comment) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'
        self.cursor.execute(sql, (item['name'], item['sku'], item['category'], item['brand'], item['model'], item['spec'], item['origin'], item['weight'], item['package'], item['price'], item['promotion_price'], item['discount'], item['comment']))
        product_id = self.cursor.lastrowid

        # 保存商品圖片
        if item['image_urls']:
            for image_url in item['image_urls']:
                self.cursor.execute('INSERT INTO image(product_id, url) VALUES(%s, %s)', (product_id, image_url))
            self.conn.commit()

        return item

這里定義了一個(gè)名為JingdongPipeline的管道,用于將抓取到的數(shù)據(jù)保存到MySQL數(shù)據(jù)庫中。在process_item方法中,首先保存商品信息到product表中,然后保存商品圖片到image表中。

(5)配置圖片下載

在項(xiàng)目的settings.py文件中,添加以下代碼:

ITEM_PIPELINES = {
    'jingdong.pipelines.JingdongPipeline': 300,
    'scrapy.pipelines.images.ImagesPipeline': 1,
}

IMAGES_STORE = 'images'

這里配置了圖片下載的管道和存儲(chǔ)路徑。

(6)運(yùn)行爬蟲

在命令行中輸入以下命令,運(yùn)行爬蟲:

scrapy crawl jingdong

這將啟動(dòng)爬蟲程序,開始抓取京東商城的商品信息、價(jià)格、評(píng)論等數(shù)據(jù),并保存到MySQL數(shù)據(jù)庫中。

五、總結(jié)

本文介紹了如何使用Python編寫爬蟲程序,抓取電商平臺(tái)的商品信息、價(jià)格、評(píng)論等數(shù)據(jù)。通過本文的學(xué)習(xí),您可以了解到Scrapy框架的基本使用方法,以及如何將抓取到的數(shù)據(jù)保存到MySQL數(shù)據(jù)庫中。同時(shí)還可以學(xué)習(xí)到如何模擬瀏覽器的行為,抓取動(dòng)態(tài)頁面的數(shù)據(jù)。希望本文對您有所幫助。

↓ ↓ ↓ 下方名片找我,各種源碼還有案例 ↓ ↓ ↓

【Python爬蟲+數(shù)據(jù)分析】采集電商平臺(tái)數(shù)據(jù)信息,并做可視化演示文章來源地址http://www.zghlxwxcb.cn/news/detail-480083.html

到了這里,關(guān)于【Python爬蟲+數(shù)據(jù)分析】采集電商平臺(tái)數(shù)據(jù)信息,并做可視化演示的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包