数据爬取与分析
1. 基础知识
1.1 数据分析的基础知识
数据分析的一般流程:
明确目标-》采集数据-》数据清洗与分析-》绘制图表并且可视化-》得出结论
1.2 具有python特色的程序

1.3 字符串切片
1 循环打印嵌套列表:movies=[“the holy”,1975,“terry jones”,91,[“graham”, [“michael”,“john”,“gilliam”,“idle”,“haha”]]],实现以下形式的输出:
The holy
1975

2、字典值操作
有如下值集合[11,22,33,44,55,66,77,88,99,90],将所有大于66的值保存至字典的第一个key中,将小于66的值保存至第二个key的值中。即:{‘k1’:大于66的所有值,‘k2’:小于66的所有值}

2. 数据采集-爬虫的设计与实现
2.1 两条技术路线:
-
爬虫框架:scrapy 、selenium -
request库,urlib原生爬虫
2.2 爬虫概念:
-
网络爬虫 网络爬虫是一种按照一定规则,自动抓取互联信息的程序或者脚本。 由于互联网数据的多样性,资源的有限性,现在根据用户需求定向抓取相关网页并分析,已经成为当今主流爬取策略 -
爬虫的本质 模拟浏览器打开网页,获取网页中我们想要的部分数据 -
爬虫工作流程
- 观察页面特征:使用Ctrl+u查看网页源码,选中某元素,进行审查
- 请求目标网页并获得相应
- 定义信息提取规则,使用re(正则)第三方网页解析器,beautiful soup xpath bs4
- 提取网页数据并保存
-
数据通常需要通过解析,解析方式: 定义信息提取规则,使用re(正则)第三方网页解析器,beautiful soup xpath bs4
2.4 Scrapy 爬虫
2.4.1 Scrapy 爬虫的主要部件以及作用
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pHCwfals-1634735959385)(C:/Users/Lenovo/AppData/Roaming/Typora/typora-user-images/image-20211019203520100.png)]
- Scrapy Engine(引擎): 负责Spider、ItemPipeline、Downloader、Scheduler中间的通讯,信号、数据传递等。
- Scheduler(调度器): 它负责接受引擎发送过来的Request请求,并按照一定的方式进行整理排列,入队,当引擎需要时,交还给引擎。
- Downloader(下载器):负责下载Scrapy Engine(引擎)发送的所有Requests请求,并将其获取到的Responses交还给Scrapy Engine(引擎),由引擎交给Spider来处理
- Spider(爬虫):它负责处理所有Responses,从中分析提取数据,获取Item字段需要的数据,并将需要跟进的URL提交给引擎,再次进入Scheduler(调度器)
- Item Pipeline(管道):它负责处理Spider中获取到的Item,并进行进行后期处理(详细分析、过滤、存储等)的地方.
- Downloader Middlewares(下载中间件):可以自定义扩展下载功能的组件(代理、cokies等)。
- Spider Middlewares(Spider中间件):可以自定扩展和操作引擎和Spider中间通信的功能组件(比如进入Spider的Responses;和从Spider出去的Requests)
2.4.2 Scrapy 爬虫的工作流程
- 引擎从调度器中取出一个链接(URL)用于接下来的抓取
- 引擎把URL封装成一个请求(Request)传给下载器
- 下载器把资源下载下来,并封装成应答包(Response)
- 爬虫解析Response
- 解析出实体(Item),则交给实体管道进行进一步的处理
- 解析出的是链接(URL),则把URL交给调度器等待抓取
2.4.3 与传统的request爬虫对比
- scrapy是封装起来的框架,他包含了下载器,解析器,日志及异常处理,基于多线程, twisted的方式处理,对于固定单个网站的爬取开发,有优势,但是对于多网站爬取 100个网站,并发及分布式处理方面,不够灵活,不便调整与括展。
- request 是一个HTTP库, 它只是用来,进行请求,对于HTTP请求,他是一个强大的库,下载,解析全部自己处理,灵活性更高,高并发与分布式部署也非常灵活,对于功能可以更好实现
2.4.4 scrapy创建爬虫的流程
-
手动创建scrapy项目夹 p (mkdir p)  -
启动命令行,进入到该项目如 cd 到p夹下  -
scrapy startproject 项目名称   cd E:/p/项目名称进入到该项目夹后,然后执行 scrapy genspider XXX XXX -
配置信息,配置itempiplines等的设置 后面就是修改配置文件settings.py部分的配置,定义item存数据 -
编写爬虫逻辑 在我们的ptest里面编写爬虫逻辑
2.5 Selenium 爬虫框架
2.5.1 适用场景
爬取动态页面,用于web应用测试的工具,selenium测试直接在浏览器运行,可以模仿人的操作,可以有效应对反爬机制
2.5.2 安装与配置
- pip install
- 用anaconda环境
- pycharm 直接下载库
2.5.3 定位元素方式
有很多种元素定位的方法,比如基于id、name、xpath、css selector等方式来定位
-
返回单个元素:
- find_element_by_id()
- find_element_by_name()
- find_element_by_xpath()
- find_element_by_link_text()
- find_element_by_partial_link_text()
- find_element_by_tag_name()
- find_element_by_class_name()
- find_element_by_css_selector()
-
返回列表:
-
find_elements_by_name() -
find_elements_by_xpath() -
find_elements_by_link_text() -
find_elements_by_partial_link_text() -
find_elements_by_tag_name() -
find_elements_by_class_name() -
find_elements_by_css_selector() 因为id是唯一的所以我觉得不能返回列表
3 . 数据库连接与查询
3.1 Mysql数据库
3.1.1安装与配置
- 安装mysql
- 最好再安装一个mysql用户图形化管理界面比如navicate
- 配置环境变量
- 下载pymysql库
3.1.2 python操作mysql数据库
操作步骤:
- 新建数据库
- 新建表
- 连接数据库
- 向当前数据库中的表插入数据
3.2 MongoDB 数据库
3.2.1安装与配置
- 安装MongoDB
- 最好再安装一个MongoDB用户图形化管理界面比如navicate
- 配置环境变量
- 下载pymongo库
3.2.2 python操作mysql数据库
操作步骤:
- 新建连接
- 新建数据库
- 连接数据集
- 向当前数据集下插入数据
4. 数据分析
4.1 numpy
4.1.1 数组的创建
import numpy as np np.ones(5)

A=np.array([[1,2],[3,4]])

np.zeros((2,3))

4.1.2 数组的变形
a2=np.arange(8).reshape(2,4)

4.1.3 数组的计算


4.2 pandas
4.2.1 数据读写、选择、整理和描述
-
从csv中读取数据 import pandas as pd df =pd.read_csv("./数据阿巴巴巴.csv") 其实还可以读取html、txt 高级操作: df=pd.read_csv("./shujv.cvs",delimiter=",",encoding="utf-8",header=0) #delimiter以怎么样的方式来分割;解码方式utf-8;设置0行为头部 -
向csv中写入数据 df.to_csv("./ababab.csv",columns=["寶貝","价格"],index=False,header=True) 不要索引,以列头这两行导入 -
数据选取
-
数据整理   -
数据描述


4.2.2 数据分组、分割、合并和变形
-
分组 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EzhROGsp-1634735959479)(C:/Users/Lenovo/AppData/Roaming/Typora/typora-user-images/image-20211019232946860.png)]


-
分割 前闭后开  -
合并
-
dataframe合并 pd.merge(df1,df2,how='left',on="卖家")#左连接 pd.merge(df1,df2,how='outer',on="卖家")#外连接 pd.merge(df1,df2,how='right',on="卖家")#右 不指定on那个列就默认相同的选择列名相同的一列 -
索引合并  -
轴向连接 按axis=1列拼接,axis为0就是按行,默认就是行。 pd.contact([col1,col2,col3],axis=1) -
变形
4.2.3 缺失值


5. 数据可视化
5.1 matplotlib绘图操作:
-
初始化,导库,乱码,设定背景 import matplotlib import matplotlit.pylot -
生成一个画布(规定画布大小,坐标系) fig,axes =plt.subplots(1,2,figsize=(12,5)) -
绘图(形状) axes[0].hit(df,bins=20) -
自动调整样式 fig.tight_layout() 
5.2 案例




|