当前位置: 移动技术网 > IT编程>脚本编程>Python > Python爬虫入门教程 36-100 酷安网全站应用爬虫 scrapy

Python爬虫入门教程 36-100 酷安网全站应用爬虫 scrapy

2019年02月20日  | 移动技术网IT编程  | 我要评论

森威鸟,超级高科技霸主,订制窗帘

爬前叨叨

2018年就要结束了,还有4天,就要开始写2019年的教程了,没啥感动的,一年就这么过去了,今天要爬取一个网站叫做酷安,是一个应用商店,大家可以尝试从手机app爬取,不过爬取app的博客,我打算在50篇博客之后在写,所以现在就放一放啦~~~

python3爬虫入门教程

酷安网站打开首页之后是一个广告页面,点击头部的应用即可

python3爬虫入门教程

页面分析

分页地址找到,这样就可以构建全部页面信息
python3爬虫入门教程入图片描述

我们想要保存的数据找到,用来后续的数据分析
python3爬虫入门教程

python3爬虫入门教程

上述信息都是我们需要的信息,接下来,只需要爬取即可,本篇文章使用的还是scrapy,所有的代码都会在文章中出现,阅读全文之后,你就拥有完整的代码啦

import scrapy

from apps.items import appsitem  # 导入item类
import re  # 导入正则表达式类

class appsspider(scrapy.spider):
    name = 'apps'
    allowed_domains = ['www.coolapk.com']
    start_urls = ['https://www.coolapk.com/apk?p=1']
    custom_settings = {
        "default_request_headers" :{
            'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'accept-language': 'en',
            'user-agent':'mozilla/5.0 你的ua'

        }
    }

代码讲解

custom_settings 第一次出现,目的是为了修改默认setting.py 文件中的配置

    def parse(self, response):
        list_items = response.css(".app_left_list>a")
        for item in list_items:
            url = item.css("::attr('href')").extract_first()

            url = response.urljoin(url)

            yield scrapy.request(url,callback=self.parse_url)

        next_page = response.css('.pagination li:nth-child(8) a::attr(href)').extract_first()
        url = response.urljoin(next_page)
        yield scrapy.request(url, callback=self.parse)

代码讲解

  1. response.css 可以解析网页,具体的语法,你可以参照上述代码,重点阅读 ::attr('href') 和 ::text
  2. response.urljoin 用来合并url
  3. next_page 表示翻页

parse_url函数用来解析内页,本函数内容又出现了3个辅助函数,分别是self.getinfo(response),self.gettags(response)self.getappinfo(response) 还有response.css().re支持正则表达式匹配,可以匹配文字内部内容

   def parse_url(self,response):
        item = appsitem()

        item["title"] = response.css(".detail_app_title::text").extract_first()
        info = self.getinfo(response)

        item['volume'] = info[0]
        item['downloads'] = info[1]
        item['follow'] = info[2]
        item['comment'] = info[3]

        item["tags"] = self.gettags(response)
        item['rank_num'] = response.css('.rank_num::text').extract_first()
        item['rank_num_users'] = response.css('.apk_rank_p1::text').re("共(.*?)个评分")[0]
        item["update_time"],item["rom"],item["developer"] = self.getappinfo(response)

        yield item

三个辅助方法如下

    def getinfo(self,response):

        info = response.css(".apk_topba_message::text").re("\s+(.*?)\s+/\s+(.*?)下载\s+/\s+(.*?)人关注\s+/\s+(.*?)个评论.*?")
        return info

    def gettags(self,response):
        tags = response.css(".apk_left_span2")
        tags = [item.css('::text').extract_first() for item in tags]

        return tags

    def getappinfo(self,response):
        #app_info = response.css(".apk_left_title_info::text").re("[\s\s]+更新时间:(.*?)")
        body_text = response.body_as_unicode()

        update = re.findall(r"更新时间:(.*)?[<]",body_text)[0]
        rom =  re.findall(r"支持rom:(.*)?[<]",body_text)[0]
        developer = re.findall(r"开发者名称:(.*)?[<]", body_text)[0]
        return update,rom,developer

保存数据

数据传输的item在这个地方就不提供给你了,需要从我的代码中去推断一下即可,哈哈

import pymongo

class appspipeline(object):

    def __init__(self,mongo_url,mongo_db):
        self.mongo_url = mongo_url
        self.mongo_db = mongo_db

    @classmethod
    def from_crawler(cls,crawler):
        return cls(
            mongo_url=crawler.settings.get("mongo_url"),
            mongo_db=crawler.settings.get("mongo_db")
        )

    def open_spider(self,spider):
        try:
            self.client = pymongo.mongoclient(self.mongo_url)
            self.db = self.client[self.mongo_db]
            
        except exception as e:
            print(e)

    def process_item(self, item, spider):
        name = item.__class__.__name__

        self.db[name].insert(dict(item))
        return item

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

代码解读

  1. open_spider 开启爬虫时,打开mongodb
  2. process_item 存储每一条数据
  3. close_spider 关闭爬虫
  4. 重点查看本方法 from_crawler 是一个类方法,在初始化的时候,从setting.py中读取配置
spider_modules = ['apps.spiders']
newspider_module = 'apps.spiders'
mongo_url = '127.0.0.1'
mongo_db = 'kuan'

python3爬虫入门教程

得到数据

调整一下爬取速度和并发数

download_delay = 3
# the download delay setting will honor only one of:
concurrent_requests_per_domain = 8

代码走起,经过一系列的努力,得到数据啦!!!
python3爬虫入门教程

抽空写个酷安的数据分析,有需要源码的,自己从头到尾的跟着写一遍就o98k了

python3爬虫入门教程

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网