17.1 初识网络爬虫
按指定规则自动浏览和抓取网络中的信息。
17.1.1 网络爬虫简述
17.1.2 网络爬虫的分类
a, 通用网络爬虫
以叫全网爬虫,爬的范围大,对爬行速度和存储空间要求高,
刷新的页面多,通常用并行工作。
通用网络,主要是由初始化URL集合,URL列表,页面模块模块,
页面分析模块, 链接过滤模块。
b, 聚焦网络爬虫
也叫主题爬虫,应用在对特定的信息的爬取
c, 增量式网络爬虫
就是增量式更新,只对发生变换的网页爬取信息。
d, 深层网络爬虫
web页面分,分表层页面,深层页面,
表层页面,不需要提交表单,使用静态的超链接。
深层页面,是指不能由静态的超连接获取的。
深层,主要通过6个基本功能的模块
(爬虫控制器, 解析器, 表单分析器,
表单分析器, 响应分析器, LVS控制器)
17.1.3 网络爬虫的基本原理
a, 获取初始URL
b, 爬取页面获取新的URL
c, 抽取新的URl放入URL队列中
d, 读取新的URL进行网页下载
e, 是否满足停止条件,是结束,否到B,
17.2 网络爬虫的常用技术
17.2.1 python的网络请求
介绍PYhton的实现HTTP网络请求的觉的3种方式:urllib, urllib3, requests
a, urllib模块
get请求方式
import urllib.request
response = urllib.request.urlopen('http://www.baidu.com')
html = response.read()
print(html)
post请求方式
import urllib.parse
import urllib.request
data = bytes(urllib.parse.urlencode({'word':'hello'}), encoding='utf-8')
response = urllib.request.urlopen('http://httpbin.org/post', data=data)
html = response.read()
print(html)
b, rullib3模块
get请求方式
import urllib3
http = urllib3.PoolManager()
response = http.request('GET','http://www.baidu.com')
print(response.data)
post请求方式
import urllib3
http = urllib3.PoolManager()
response = http.request('POST', 'http://httpbin.org/post', fields={'word':'hello'})
print(response.data)
c, requests模块
get请求方式
import requests
response = requests.get('http://www.baidu.com')
print(response.status_code)
print(response.url)
print(response.headers)
print(response.cookies)
print(response.text)
print(response.content)
post请求方式
import requests
data = {'word':'hello'}
response = requests.post('http://httpbin.org//post', data = data)
print(response.content)
print(response.status_code)
17.2.2 请求Headers处理
火狐,进入网站,然后,按ctrl+shift+E,进入网络监视器,打开第一个GET请求,
找到第一个get请求,找到第一个use-agent,进入如下操作
import requests
string = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0'
header = {'rser-agent':string}
url = 'https://www.baidu.com/'
response = requests.get(url, headers = header)
res = response.content.decode('utf-8')
res = res.replace('<','\n<')
print(res)
17.2.3 网络超时
import requests
from requests.exceptions import ReadTimeout, HTTPError, RequestException
for a in range(0, 50):
try:
response = requests.get('http://www.baidu.com', timeout=0.05)
print(response.status_code)
except ReadTimeout as e:
print("超时异常 : ", e)
# 浏览器无法连接到服务器而增加出来的错误提示
except HTTPError as e:
print("HTTP异常 : ", e)
# 请求错误
# https: // zhidao.baidu.com / question / 240298462.html
except RequestException as e:
print("requests 错误 : ", e)
17.2.4 代理服务
import requests
proxy_1 = {'http' : '123.249.28.107:3128', "https" : '114.95.227.102:8118'}
# 获取免费代理IP, https://www.xicidaili.com/?t=253
response = requests.get('http://www.baidu.com', proxies = proxy_1)
print(response.content)
17.2.5 HTTP解析之BeautifulSoup
a, BeautifelSoup的安装
pip install bs4 # 从HTTP,XML 中提取数据的python库
lxml # lxml解析器
html5lib # html解析器
b, BeautifelSoup的使用
from bs4 import BeautifulSoup
import requests
response = requests.get('https://tieba.baidu.com/index.html')
string = response.content.decode('utf-8')
string = string.replace("<", "\n<")
soup = BeautifulSoup(string, 'html5lib')
print(soup)
17.3 网络爬虫开发常用框架
17.3.1 Scrapy爬虫框架(免费开源的)
官网地址:https://scrapy.org
17.3.2 Crawley 爬虫框架
17.3.3 PySpider 爬虫框架
17.4 实战项目:快手爬票
17.4.1 快手爬票概述
17.4.2 搭建QT环境
17.4 实战项目 :快手爬票
17.4.2 搭建QT环境
第一步: 安装QT软件;
第二步: 需要python的 PyQt5 和 PyQt5-tools
第三步: 在pytharm 中扩展工具
用来启动QT的扩展工具
name: QTDesigner
program: C:\Users\juliciy\AppData\Local\Programs\Python\Python36\Lib\site-packages\pyqt5_tools\designer.exe
working dir: $FileDir$
用来转换UI到PY的工具
name: PyUIC
program: C:\Users\juliciy\AppData\Local\Programs\Python\Python36\python.exe
arguments:-m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
working dir: $FileDir$
用来处理QT传过来的图片格式qrc 的
name: qrcTory
program: C:\Users\juliciy\AppData\Local\Programs\Python\Python36\Scripts\pyrcc5.exe
arguments: $FileName$ -o $FileNameWithoutExtension$_rc.py
working dir: $FileDir$
第四步:在QT工具中调好窗口,设置图片,记得添加时要选择添加样式,再背景图片;
第五步:导入转换即可
第六步: 在转换的代码后添加如下代码,即可测试显示
import sys
def show_MainWindow():
app = QtWidgets.QApplication(sys.argv) # 首先必须实例化QApplication类,作为GUI主程序入口
MainWindow = QtWidgets.QMainWindow() # 实例化QtWidgets.QMainWindow类,创建自带menu的窗体类型QMainWindow
ui = Ui_MainWindow() # 实例UI类
ui.setupUi(MainWindow) # 设置窗体UI
MainWindow.show() # 显示窗体
sys.exit(app.exec_()) # 当来自操作系统的分发事件指派调用窗口时,
# 应用程序开启主循环(mainloop)过程,
# 当窗口创建完成,需要结束主循环过程,
# 这时候呼叫sys.exit()方法来,结束主循环过程退出,
# 并且释放内存。为什么用app.exec_()而不是app.exec()?
# 因为exec是python系统默认关键字,为了以示区别,所以写成exec_
import imge_rc
if __name__ == "__main__":
show_MainWindow() # 调用显示窗体的方法