一、移动端数据的爬取

  • 基于某一款抓包工具,fiddler,青花瓷,miteproxy
  • fillder进行一个基本的配置:tools->options->connection->allow remote ...
  • http://fillder所在pc机的ip:58083/:访问到一张提供了证书下载功能的页面
  • fiddler所在的机器和手机在同一网段下:在手机浏览器中访问http://fillder所在pc机的ip:58083/
  • 获取子页面进行证书的下载和安装(证书信任的操作)
  • 配置你的手机的代理:将手机的代理配置成fiddler所对应pc机的ip和fillder自己的端口
  • 就可以让fiddler捕获手机发起的http和https的请求

二、scrapy,pyspider

  • 什么是框架?如何学习框架?
  • 就是一个集成了各种功能且具有很强通用性(可以被应用在各种不同的需求中)的一个项目模板.
  • 我们只需要学习框架中封装好的相关功能的使用即可.

1.scrapy集成了哪些功能:

  • 高性能的数据解析操作,持久化存储操作,高性能的数据下载的操作.....

2.环境的安装:

  1. pip3 install wheel
  2. 下载twisted http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted
  3. 进入下载目录,执行 pip3 install Twisted‑17.1.0‑cp35‑cp35m‑win_amd64.whl
  4. pip3 install pywin32
  5. pip3 install scrapy

3.scrapy的基本使用

  1. 创建一个工程:scrapy startproject firstBlood
  2. 必须在spiders这个目录下创建一个爬虫文件
  3. cd proName
  4. scrapy genspider spiderName www.xxx.com
  5. 执行工程:scrapy crawl spiderName
  6. settings.py:
  7. 不遵从robots协议
  8. 进行UA伪装
  9. 进行日志等级设定:LOG_LEVEL = 'ERROR'

4.持久化存储:

  • 基于终端指令:

    • 特性:只可以将parse方法的返回值存储到本地的磁盘文件中
    • 指令:scrapy crawl spiderName -o filePath
  • 基于管道:实现流程

    1.数据解析
    2.在item类中定义相关的属性
    3.将解析的数据存储或者封装到一个item类型的对象(items文件中对应类的对象)
    4.向管道提交item
    5.在管道文件的process_item方法中接收item进行持久化存储
    6.在配置文件中开启管道

4.1将同一份数据持久化到不同的平台中?

  • 分析:

    1.管道文件中的一个管道类负责数据的一种形式的持久化存储
    2.爬虫文件向管道提交的item只会提交给优先级最高的那一个管道类
    3.在管道类的process_item中的return item表示的是将当前管道接收的item返回/提交给下一个即将被执行的管道类

5.在scrapy中如何进行手动请求发送(GET)

  • 使用场景:爬取多个页码对应的页面源码数据
  • yield scrapy.Request(url,callback)
  • 在scrapy中如何进行手动请求发送(POST)
  • data = { #post请求的请求参数
    'kw':'aaa'
    }
    yield scrapy.FormRequest(url,formdata=data,callback)

6.scrapy五大核心组件的工作流程:

  • 引擎(Scrapy)

    • 用来处理整个系统的数据流处理, 触发事务(框架核心)
  • 调度器(Scheduler)
    • 用来接受引擎发过来的请求, 压入队列中, 并在引擎再次请求的时候返回. 可以想像成一个URL(抓取网页的网址或者说是链接)的优先队列, 由它来决定下一个要抓取的网址是什么, 同时去除重复的网址
  • 下载器(Downloader)
    • 用于下载网页内容, 并将网页内容返回给蜘蛛(Scrapy下载器是建立在twisted这个高效的异步模型上的)
  • 爬虫(Spiders)
    • 爬虫是主要干活的, 用于从特定的网页中提取自己需要的信息, 即所谓的实体(Item)。用户也可以从中提取出链接,让Scrapy继续抓取下一个页面
  • 项目管道(Pipeline)
    • 负责处理爬虫从网页中抽取的实体,主要的功能是持久化实体、验证实体的有效性、清除不需要的信息。当页面被爬虫解析后,将被发送到项目管道,并经过几个特定的次序处理数据。

 1 # -*- coding: utf-8 -*-
2 import scrapy
3 from qiubaiPro.items import QiubaiproItem
4
5 class QiubaiSpider(scrapy.Spider):
6 name = 'qiubai'
7 # allowed_domains = ['www.xxx.com']
8 start_urls = ['https://www.qiushibaike.com/text/']
9 def start_requests(self):
10 for url in self.start_urls:
11 yield scrapy.Request(url,callback=self.parse)
12 #基于终端指令的持久化存储操作
13 # def parse(self, response):
14 # div_list = response.xpath('//*[@id="content-left"]/div')
15 # all_data = []
16 # for div in div_list:
17 # #scrapy中的xpath返回的列表的列表元素一定是Selector对象,我们最终想要的解析的
18 # #数据一定是存储在该对象中
19 # #extract()将Selector对象中data参数的值取出
20 # # author = div.xpath('./div[1]/a[2]/h2/text()')[0].extract()
21 # author = div.xpath('./div[1]/a[2]/h2/text()').extract_first()
22 # #列表直接调用extract表示的是将extract作用到每一个列表元素中
23 # content = div.xpath('./a[1]/div/span//text()').extract()
24 # content = ''.join(content)
25 # dic = {
26 # 'author':author,
27 # 'content':content
28 # }
29 # all_data.append(dic)
30 # return all_data
31 #基于管道的持久化存储
32 # def parse(self, response):
33 # div_list = response.xpath('//*[@id="content-left"]/div')
34 # all_data = []
35 # for div in div_list:
36 # #scrapy中的xpath返回的列表的列表元素一定是Selector对象,我们最终想要的解析的
37 # #数据一定是存储在该对象中
38 # #extract()将Selector对象中data参数的值取出
39 # # author = div.xpath('./div[1]/a[2]/h2/text()')[0].extract()
40 # author = div.xpath('./div[1]/a[2]/h2/text()').extract_first()
41 # #列表直接调用extract表示的是将extract作用到每一个列表元素中
42 # content = div.xpath('./a[1]/div/span//text()').extract()
43 # content = ''.join(content)
44 #
45 # #将解析的数据存储到item对象
46 # item = QiubaiproItem()
47 # item['author'] = author
48 # item['content'] = content
49 #
50 # #将item提交给管道
51 # yield item #item一定是提交给了优先级最高的管道类
52
53 #将多个页码对应的页面数据进行爬取和解析的操作
54 url = 'https://www.qiushibaike.com/text/page/%d/'#通用的url模板
55 pageNum = 1
56 #parse第一次调用表示的是用来解析第一页对应页面中的段子内容和作者
57 def parse(self, response):
58 div_list = response.xpath('//*[@id="content-left"]/div')
59 all_data = []
60 for div in div_list:
61 # scrapy中的xpath返回的列表的列表元素一定是Selector对象,我们最终想要的解析的
62 # 数据一定是存储在该对象中
63 # extract()将Selector对象中data参数的值取出
64 # author = div.xpath('./div[1]/a[2]/h2/text()')[0].extract()
65 author = div.xpath('./div[1]/a[2]/h2/text()').extract_first()
66 # 列表直接调用extract表示的是将extract作用到每一个列表元素中
67 content = div.xpath('./a[1]/div/span//text()').extract()
68 content = ''.join(content)
69
70 # 将解析的数据存储到item对象
71 item = QiubaiproItem()
72 item['author'] = author
73 item['content'] = content
74
75 # 将item提交给管道
76 yield item # item一定是提交给了优先级最高的管道类
77
78 if self.pageNum <= 5:
79 self.pageNum += 1
80 new_url = format(self.url%self.pageNum)
81 #手动请求(get)的发送
82 yield scrapy.Request(new_url,callback=self.parse)

qiubai.py

# -*- coding: utf-8 -*-

# Scrapy settings for qiubaiPro project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
# https://docs.scrapy.org/en/latest/topics/settings.html
# https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
# https://docs.scrapy.org/en/latest/topics/spider-middleware.html BOT_NAME = 'qiubaiPro'
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36' SPIDER_MODULES = ['qiubaiPro.spiders']
NEWSPIDER_MODULE = 'qiubaiPro.spiders' # Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'qiubaiPro (+http://www.yourdomain.com)' # Obey robots.txt rules
ROBOTSTXT_OBEY = False
LOG_LEVEL = 'ERROR' # 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://docs.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://docs.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
# 'qiubaiPro.middlewares.QiubaiproSpiderMiddleware': 543,
#} # Enable or disable downloader middlewares
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
# 'qiubaiPro.middlewares.QiubaiproDownloaderMiddleware': 543,
#} # Enable or disable extensions
# See https://docs.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
# 'scrapy.extensions.telnet.TelnetConsole': None,
#} # Configure item pipelines
# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
'qiubaiPro.pipelines.QiubaiproPipeline': 300, #300表示的是优先级
# 'qiubaiPro.pipelines.MysqlPL': 301,
# 'qiubaiPro.pipelines.RedisPL': 302,
} # Enable and configure the AutoThrottle extension (disabled by default)
# See https://docs.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://docs.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'

settings

# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html import scrapy class QiubaiproItem(scrapy.Item):
# define the fields for your item here like:
author = scrapy.Field() #Field可以将其理解成是一个万能的数据类型
content = scrapy.Field()

items.py

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html import pymysql
from redis import Redis
class QiubaiproPipeline(object):
fp = None
def open_spider(self,spider):
print('开始爬虫......')
self.fp = open('qiushibaike.txt','w',encoding='utf-8') #使用来接收爬虫文件提交过来的item,然后将其进行任意形式的持久化存储
#参数item:就是接收到的item对象
#该方法每接收一个item就会调用一次
def process_item(self, item, spider):
author = item['author']
content= item['content'] self.fp.write(author+':'+content+'\n')
return item #item是返回给了下一个即将被执行的管道类 def close_spider(self,spider):
print('结束爬虫!')
self.fp.close() #负责将数据存储到mysql
class MysqlPL(object):
conn = None
cursor = None
def open_spider(self,spider):
self.conn = pymysql.Connect(host='127.0.0.1',port=3306,user='root',password='123',db='spider',charset='utf8')
print(self.conn)
def process_item(self,item,spider):
author = item['author']
content = item['content'] sql = 'insert into qiubai values ("%s","%s")'%(author,content)
self.cursor = self.conn.cursor()
try:
self.cursor.execute(sql)
self.conn.commit()
except Exception as e:
print(e)
self.conn.rollback()
return item
def close_spider(self,spider):
self.cursor.close()
self.conn.close() class RedisPL(object):
conn = None
def open_spider(self,spider):
self.conn = Redis(host='127.0.0.1',port=6379)
print(self.conn)
def process_item(self,item,spider):
self.conn.lpush('all_data',item)
#注意:如果将字典写入redis报错:pip install -U redis==2.10.6

pipelines.py

爬虫必知必会(5)_scrapy框架_基础的更多相关文章

  1. python网络爬虫,知识储备,简单爬虫的必知必会,【核心】

    知识储备,简单爬虫的必知必会,[核心] 一.实验说明 1. 环境登录 无需密码自动登录,系统用户名shiyanlou 2. 环境介绍 本实验环境采用带桌面的Ubuntu Linux环境,实验中会用到桌 ...

  2. Django框架之第六篇(模型层)--单表查询和必知必会13条、单表查询之双下划线、Django ORM常用字段和参数、关系字段

    单表查询 补充一个知识点:在models.py建表是 create_time = models.DateField() 关键字参数: 1.auto_now:每次操作数据,都会自动刷新当前操作的时间 2 ...

  3. 2015 前端[JS]工程师必知必会

    2015 前端[JS]工程师必知必会 本文摘自:http://zhuanlan.zhihu.com/FrontendMagazine/20002850 ,因为好东东西暂时没看懂,所以暂时保留下来,供以 ...

  4. [ 学习路线 ] 2015 前端(JS)工程师必知必会 (2)

    http://segmentfault.com/a/1190000002678515?utm_source=Weibo&utm_medium=shareLink&utm_campaig ...

  5. Android程序员必知必会的网络通信传输层协议——UDP和TCP

    1.点评 互联网发展至今已经高度发达,而对于互联网应用(尤其即时通讯技术这一块)的开发者来说,网络编程是基础中的基础,只有更好地理解相关基础知识,对于应用层的开发才能做到游刃有余. 对于Android ...

  6. 迈向高阶:优秀Android程序员必知必会的网络基础

    1.前言 网络通信一直是Android项目里比较重要的一个模块,Android开源项目上出现过很多优秀的网络框架,从一开始只是一些对HttpClient和HttpUrlConnection简易封装使用 ...

  7. 脑残式网络编程入门(三):HTTP协议必知必会的一些知识

    本文原作者:“竹千代”,原文由“玉刚说”写作平台提供写作赞助,原文版权归“玉刚说”微信公众号所有,即时通讯网收录时有改动. 1.前言 无论是即时通讯应用还是传统的信息系统,Http协议都是我们最常打交 ...

  8. RecyclerView 必知必会(转)

    [腾讯Bugly干货分享]RecyclerView 必知必会 本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:http://mp.weixin.qq.com ...

  9. H5系列之History(必知必会)

    H5系列之History(必知必会)   目录 概念 兼容性 属性 方法 H5方法       概念     理解History Api的使用方式 目的是为了解决哪些问题   作用:ajax获取数据时 ...

随机推荐

  1. Jenkins管理员密码忘记修改操作

    一.Jenkins管理员密码忘记 当jenkins忘记了管理用户的密码时,只能通过修改配置文件并重启的方式初始化设置用户名及密码,操作如下: [root@localhost jenkins]# vim ...

  2. SPOJ VLATTICE Visible Lattice Points(莫比乌斯反演)题解

    题意: 有一个\(n*n*n\)的三维直角坐标空间,问从\((0,0,0)\)看能看到几个点. 思路: 按题意研究一下就会发现题目所求为. \[(\sum_{i=1}^n\sum_{j=1}^n\su ...

  3. 牛客多校第九场H Cutting Bamboos(主席树 区间比k小的个数)题解

    题意: 标记为\(1-n\)的竹子,\(q\)个询问,每次给出\(l,r,x,y\).要求为砍区间\(l,r\)的柱子,要求砍\(y\)次把所有竹子砍完,每次砍的时候选一个高度,把比他高的都砍下来,并 ...

  4. 解决: "E: 无法获得锁 /var/lib/dpkg/lock - open (11: 资源暂时不可用) ..."

    解决: "E: 无法获得锁 /var/lib/dpkg/lock - open (11: 资源暂时不可用) E: 无法锁定管理目录(/var/lib/dpkg/),是否有其他进程正占用它?& ...

  5. Linux cp command All In One

    Linux cp command All In One $ man cp $ cp -h # 强制 $ cp -f # 递归,复制文件夹 $ cp -r demos cp -fr # ./folder ...

  6. Front End Frameworks Trending 2021

    Front End Frameworks Trending 2021 Front End Frameworks https://2019.stateofjs.com/front-end-framewo ...

  7. TypeScript TSConfig All In One

    TypeScript TSConfig All In One tsconfig.json https://www.typescriptlang.org/tsconfig https://www.typ ...

  8. GeoJSON feature & Mapbox segments

    GeoJSON feature & Mapbox segments custom JSON format ??? { "rows": [], "props&quo ...

  9. py pandas

    import pandas as pd class Main(): def __init__(self): # 读取excel self.df = pd.read_excel("C:\\Us ...

  10. PM2 in depth

    PM2 in depth ecosystem.config.js module.exports = { apps : [{ name: "app", script: ". ...