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

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

爬前叨叨

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

image

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

image

页面分析

分页地址找到,这样就可以构建全部页面信息
image

我们想要保存的数据找到,用来后续的数据分析
image

image

上述信息都是我们需要的信息,接下来,只需要爬取即可,本篇文章使用的还是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'

image

得到数据

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

DOWNLOAD_DELAY = 3
# The download delay setting will honor only one of:
CONCURRENT_REQUESTS_PER_DOMAIN = 8

代码走起,经过一系列的努力,得到数据啦!!!
image

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

image

相关文章
|
5月前
|
数据采集 Web App开发 数据安全/隐私保护
实战:Python爬虫如何模拟登录与维持会话状态
实战:Python爬虫如何模拟登录与维持会话状态
|
6月前
|
数据采集 Web App开发 自然语言处理
新闻热点一目了然:Python爬虫数据可视化
新闻热点一目了然:Python爬虫数据可视化
|
6月前
|
监控 数据可视化 数据挖掘
Python Rich库使用指南:打造更美观的命令行应用
Rich库是Python的终端美化利器,支持彩色文本、智能表格、动态进度条和语法高亮,大幅提升命令行应用的可视化效果与用户体验。
526 0
|
5月前
|
数据采集 监控 数据库
Python异步编程实战:爬虫案例
🌟 蒋星熠Jaxonic,代码为舟的星际旅人。从回调地狱到async/await协程天堂,亲历Python异步编程演进。分享高性能爬虫、数据库异步操作、限流监控等实战经验,助你驾驭并发,在二进制星河中谱写极客诗篇。
Python异步编程实战:爬虫案例
|
6月前
|
数据采集 存储 XML
Python爬虫技术:从基础到实战的完整教程
最后强调: 父母法律法规限制下进行网络抓取活动; 不得侵犯他人版权隐私利益; 同时也要注意个人安全防止泄露敏感信息.
919 19
|
5月前
|
数据采集 存储 JSON
Python爬虫常见陷阱:Ajax动态生成内容的URL去重与数据拼接
Python爬虫常见陷阱:Ajax动态生成内容的URL去重与数据拼接
|
6月前
|
机器学习/深度学习 算法 安全
【强化学习应用(八)】基于Q-learning的无人机物流路径规划研究(Python代码实现)
【强化学习应用(八)】基于Q-learning的无人机物流路径规划研究(Python代码实现)
490 6
|
5月前
|
数据采集 存储 JavaScript
解析Python爬虫中的Cookies和Session管理
Cookies与Session是Python爬虫中实现状态保持的核心。Cookies由服务器发送、客户端存储,用于标识用户;Session则通过唯一ID在服务端记录会话信息。二者协同实现登录模拟与数据持久化。
|
6月前
|
数据采集 存储 Web App开发
处理Cookie和Session:让Python爬虫保持连贯的"身份"
处理Cookie和Session:让Python爬虫保持连贯的"身份"
|
6月前
|
数据采集 Web App开发 前端开发
处理动态Token:Python爬虫应对AJAX授权请求的策略
处理动态Token:Python爬虫应对AJAX授权请求的策略

推荐镜像

更多