17Python爬虫---Scrapy爬取当当网特产

一、总体思路

1、创建scrapy项目
2、分析当当网特产网址
3、分析出所取部分xpath公式
4、编写item
5、编写爬虫
6、编写pipline文件将取到的数据存入到文件中

二、具体实践

1、创建scrapy项目
scrapy startproject autopjt
2、分析当当网特产网址
第一页
http://category.dangdang.com/cid4011029.html
第二页
http://category.dangdang.com/pg2-cid4011029.html
第三页
http://category.dangdang.com/pg3-cid4011029.html

可以看出pg后面跟的为第几页
那么 我们可以把第一页改成
http://category.dangdang.com/pg1-cid4011029.html发现和第一页一样,那么就可以找出规律
实际中使用的url为
"http://category.dangdang.com/pg"+ i +"-cid4011029.html"
i为第几页

3、分析出所取部分xpath公式
# 标题  链接
<a title=" 【贵阳馆】贵州特产 从江椪柑芦柑新鲜水果地标产品央视精准扶贫广告产品贵州特产5斤装_2.5KG包邮" ddclick="act=normalResult_picture&pos=1207074368_0_1_m" class="pic" name="itemlist-picture" dd_name="单品图片" href="http://product.dangdang.com/1207074368.html" target="_blank"><img src="http://img3m8.ddimg.cn/38/21/1207074368-1_b_3.jpg" alt=" 【贵阳馆】贵州特产 从江椪柑芦柑新鲜水果地标产品央视精准扶贫广告产品贵州特产5斤装_2.5KG包邮">a>

# 价格 //span[@class="price_n"]/text()
<p class="price"> <span class="price_n">¥16.80span>p>

# 评论数
<a href="http://product.dangdang.com/1207074368.html?point=comment_point" target="_blank" name="itemlist-review" dd_name="单品评论" ddclick="act=click_review_count&pos=1207074368_0_1_m">198条评论a>

由此可以推断出xpath为

# 价格  //span[@class='price_n']/text()
# 标题  //a[@class='pic']/@title
# 链接  //a[@class='pic']/@href
# 评论数 //a[@name='itemlist-review']/text()
4、项目代码

项目结构
项目结构

(1)item
# -*- coding: utf-8 -*-
import scrapy

class AutopjtItem(scrapy.Item):
    # 定义好name用来存储商品
    name = scrapy.Field()
    # 定义好price用来存储商品价格
    price = scrapy.Field()
    # 定义好link用来存储商品链接
    link = scrapy.Field()
    # 定义好comnum用来存储商品评论数
    comnum = scrapy.Field()
(2)AutospdSpider

创建spider文件scrapy genspider -t basic autospd dangdang.com

# -*- coding: utf-8 -*-
import scrapy
from autopjt.items import AutopjtItem
from scrapy.http.request import Request

# 价格    //span[@class='price_n']/text()
# 标题    //a[@class='pic']/@title
# 链接    //a[@class='pic']/@href
# 评论数  //a[@name='itemlist-review']/text()

class AutospdSpider(scrapy.Spider):
    name = 'autospd'
    allowed_domains = ['dangdang.com']
    start_urls = [
        'http://category.dangdang.com/pg1-cid4011029.html'
    ]

    def parse(self, response):
        item = AutopjtItem()
        # 通过XPath表达式分别提取商品的名称、价格、链接、评论数等信息
        item['name'] = response.xpath("//a[@class='pic']/@title").extract()
        item['price'] = response.xpath("//span[@class='price_n']/text()").extract()
        item['link'] = response.xpath("//a[@class='pic']/@href").extract()
        item['comnum'] = response.xpath("//a[@name='itemlist-review']/text()").extract()
        # 提取完后返回item
        yield item
        # 接下来很关键,通过循环自动爬去75页的数据
        for i in range(1, 76):
            url = "http://category.dangdang.com/pg" + str(i) + "-cid4011029.html"
            # 通过yield返回Request,并制定要爬取的网址和回调函数
            # 实现自动爬取
            yield Request(url, callback=self.parse)
(3)piplines
# -*- coding: utf-8 -*-
import json
import codecs

class AutopjtPipeline(object):
    def __init__(self):
        self.file = codecs.open("C:/Users/Administrator/Desktop/dangdangdate.json", "wb", encoding="utf-8")

    def process_item(self, item, spider):
        # i = json.dumps(dict(item), ensure_ascii=False)
        # # 每行数据后加上换行
        # line = i + "\n"
        # # 将数据写入到dangdangdate.json文件中
        # self.file.write(line)
        # return item
        for j in range(0, len(item['name'])):
            # 将当前页的第j个商品的名称赋值给变量name
            name = item["name"][j]
            price = item["price"][j]
            link = item["link"][j]
            comnum = item["comnum"][j]
            # 将当前页下第j个商品的name、price、link、comnum等信息处理一下
            # 重新组合成一个字典
            goods = {"name": name, "price": price, "link": link, "comnum": comnum}
            # 将当前页下第j个产品的数据写入json文件
            i = json.dumps(dict(goods), ensure_ascii=False)
            line = i + "\n"
            self.file.write(line)

    def close_spider(self, spider):
        # 关闭dangdangdate.json文件
        self.file.close()
(4)settings.py
# -*- coding: utf-8 -*-

# Scrapy settings for autopjt project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     https://doc.scrapy.org/en/latest/topics/settings.html
#     https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#     https://doc.scrapy.org/en/latest/topics/spider-middleware.html

BOT_NAME = 'autopjt'

SPIDER_MODULES = ['autopjt.spiders']
NEWSPIDER_MODULE = 'autopjt.spiders'


# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'autopjt (+http://www.yourdomain.com)'

# Obey robots.txt rules
ROBOTSTXT_OBEY = True

# Configure maximum concurrent requests performed by Scrapy (default: 16)
#CONCURRENT_REQUESTS = 32

# Configure a delay for requests for the same website (default: 0)
# See https://doc.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
#DOWNLOAD_DELAY = 3
# The download delay setting will honor only one of:
#CONCURRENT_REQUESTS_PER_DOMAIN = 16
#CONCURRENT_REQUESTS_PER_IP = 16

# Disable cookies (enabled by default)
COOKIES_ENABLED = False

# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False

# Override the default request headers:
#DEFAULT_REQUEST_HEADERS = {
#   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#   'Accept-Language': 'en',
#}

# Enable or disable spider middlewares
# See https://doc.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
#    'autopjt.middlewares.AutopjtSpiderMiddleware': 543,
#}

# Enable or disable downloader middlewares
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
#    'autopjt.middlewares.AutopjtDownloaderMiddleware': 543,
#}

# Enable or disable extensions
# See https://doc.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
#    'scrapy.extensions.telnet.TelnetConsole': None,
#}

# Configure item pipelines
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
   'autopjt.pipelines.AutopjtPipeline': 300,
}

# Enable and configure the AutoThrottle extension (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

注意:如果出现robots错误 把这里的True改为Fasle即可解决

三、运行及结果展示

scrapy crawl autospd --nolog
打开dangdangdata.json文件,结果如下
这里写图片描述

你可能感兴趣的:(Python,python爬虫)