#yyds干货盘点#Python爬虫之Urllib用法合集
Posted 是Dream呀
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了#yyds干货盘点#Python爬虫之Urllib用法合集相关的知识,希望对你有一定的参考价值。
@TOC
一、何为爬虫
网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本。
我们平时的上网就是浏览器提交请求->下载网页代码->解析/渲染成页面。而我们的爬虫就是模拟浏览器发送请求->下载网页代码->只提取有用的数据->存放于数据库或文件中。所以,我们的爬虫程序只提取网页代码中对我们有用的数据。
如果我们把互联网比作一张大的蜘蛛网,那计算机上的数据便是蜘蛛网上的一个猎物,而`爬虫程序就是一只小蜘蛛,沿着蜘蛛网抓取自己想要的数据。
`
二、爬虫核心
三、爬虫的用途
1.数据分析 / 人工数据集
2.社交软件冷启动
3.舆情控制
4.竞争对手监控
四、爬虫分类
1.通用爬虫:
实例: 百度、360、google、sougou等搜索引擎‐‐‐伯乐在线
功能: 访问网页‐>抓取数据‐>数据存储‐>数据处理‐>提供检索服务
robots协议 一个约定俗成的协议,添加robots.txt文件,来说明本网站哪些内容不可以被抓取,起不到限制作用 自己写的爬虫无需遵守
网站排名(SEO)
- 根据pagerank算法值进行排名(参考个网站流量、点击率等指标)
- 百度竞价排名
缺点:
1.抓取的数据大多是无用的
2.不能根据用户的需求来精准获取数据
2.聚焦爬虫
功能: 根据需求,实现爬虫程序,抓取需要的数据
设计思路:
1.确定要爬取的url 如何获取Url
2.模拟浏览器通过http协议访问url,获取服务器返回的html代码 如何访问
3.解析html字符串(根据一定规则提取需要的数据) 如何解析
五、反爬手段
1.User‐Agent:
User Agent中文名为用户代理,简称 UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版,urllib库使用 7.请求对象的定制 扩展:编码的由来 User Agent中文名为用户代理,简称 UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版 本、CPU 类型、浏览器及版本、浏览器渲染引擎、浏览器语言、浏览器插件等。
2.代理IP
西次代理
快代理
什么是高匿名、匿名和透明代理?它们有什么区别?
1.使用透明代理,对方服务器可以知道你使用了代理,并且也知道你的真实IP。
2.使用匿名代理,对方服务器可以知道你使用了代理,但不知道你的真实IP。
3.使用高匿名代理,对方服务器不知道你使用了代理,更不知道你的真实IP。
3.验证码访问
打码平台 :云打码平台
4.动态加载网页
网站返回的是js数据 并不是网页的真实数据
selenium驱动真实的浏览器发送请求
5.数据加密
分析js代码
六、.urllib库使用
1.爬取网站界面
urllib.request.urlopen() 模拟浏览器向服务器发送请求
response 服务器返回的数据 response的数据类型是HttpResponse
# -*-coding:utf-8 -*-
# @Author:到点了,心疼徐哥哥
# 奥利给干!!!
#
# 使用urllib获取百度首页源码
import urllib.request
# 1.定义一个url 就是要访问的地址
url = https://www.bilibili.com/
# 2.模拟浏览器向服务器发送请求 response响应
response = urllib.request.urlopen(url)
# 3.获取响应中的页面中的源码 content内容
# read方法 返回的是字节形式的二进制字
# 将二进制的数据转换为字符串
# 二进制到字符串 解码! decode(编码的格式)
content = response.read().decode(utf-8)
# 打印数据
print(content)
2.一个类型,六个方法
# -*-coding:utf-8 -*-
# @Author:到点了,心疼徐哥哥
# 奥利给干!!!
import urllib.request
url = https://www.bilibili.com/
# 模拟浏览器向服务器发送请求
response = urllib.request.urlopen(url)
# 一个类型:HTTPResponse
# 六个方法:read readline readlines getcode geturl getheader
# 返回多少个字节
# content = response.read(5)
# print(content)
# 读取一行
# content=response.readline()
# print(content)
# 一行一行的读,直至读完
# content=response.readlines()
# print(content)
# 返回状态码 如果是200,就证明我们的逻辑没有错误
# print(response.getcode())
# 返回url地址
# print(response.geturl())
# 获取一个状态信息
print(response.getheaders())
3.下载网页、图片and视频
网页:
import urllib.request
# 下载网页
# url_page=https://www.bilibili.com/
#
# url代表的是下载的路径 filename是文件的名字
# 在Python中 可以是变量的名字也可以是直接写值
# urllib.request.urlretrieve(url_page,bilibili.html) # 后缀很重要
图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zULSbS7wyadIY7sxwySxwkDO4uildrKQytnlSDWWRivqDxjqU2j2omtY4A8yqZS8YbzcKF+fpn5QBz2FeIfGPTbC01ywvLW2itW1Gy+0XEcC7IfNWVk3InO3cAN3OCea4px5TvlRtRVS/qjjqM0Ud6kmwUUUhNAj//2Q==
# urllib.request.urlretrieve(url=url_img,filename=xzq.jpg)
视频:
# 下载视频
url_video=https://vd2.bdstatic.com/mda-mj01i9fur5bzkwj7/sc/cae_h264/1633050469983308417/mda-mj01i9fur5bzkwj7.mp4?v_from_s=hkapp-haokan-hnb&auth_key=1633070432-0-0-e7d194ed5654813f4560e2184fbff9e0&bcevod_channel=searchbox_feed&pd=1&pt=3&abtest=
urllib.request.urlretrieve(url_video,阅兵.mp4)
七.请求对象的定制
UA介绍:User Agent中文名为用户代理
,简称 UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统 及版本、CPU 类型、浏览器及版本。浏览器内核、浏览器渲染引擎、浏览器语言、浏览器插件等
编码的由来:
# -*-coding:utf-8 -*-
# @Author:到点了,心疼徐哥哥
# 奥利给干!!!
import urllib.request
import urllib.parse
url = https://www.baidu.com/s?wd=
# 请求对象的定制为了解决反爬的第一种手段
headers =
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36
# 将周杰伦三个字变为unicode编码的格式
# 我们需要依赖于urllib.parse
name = urllib.parse.quote(薛之谦)
url = url + name
# print(url)
# 请求对象的定制
request = urllib.request.Request(url=url,headers=headers)
# 模拟浏览器向服务器发送请求
response = urllib.request.urlopen(request)
# 获取响应的内容
content = response.read().decode(utf-8)
# 打印数据
print(content)
2.get请求方式:urllib.parse.urlencode()
# -*-coding:utf-8 -*-
# @Author:到点了,心疼徐哥哥
# 奥利给干!!!
# urlencode 应用场景:多个参数的时候
import urllib.request
import urllib.parse
base_url=https://www.baidu.com/s?
data =
wd:周杰伦,
sex:男
new_data = urllib.parse.urlencode(data)
# 请求资源路径
url = base_url +new_data
# print(new_data)
headers =
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36
# 请求对象的定制
request = urllib.request.Request(url=url,headers=headers)
# 模拟浏览器向服务器发送请求
response = urllib.request.urlopen(request)
# 获取网页源码的数据
content = response.read().decode(utf-8)
print(content)
3.post请求方式
# -*-coding:utf-8 -*-
# @Author:到点了,心疼徐哥哥
# 奥利给干!!!
import urllib.request
import urllib.parse
url = https://fanyi.baidu.com/sug
headers =
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36
data =
kw:spider
# post请求的参数 必须进行编码
data = urllib.parse.urlencode(data).encode(utf8)
# post请求的参数,不会拼接在url的后立案,需要放在请求对象定制的参数中
request = urllib.request.Request(url=url,data=data,headers=headers)
# 模拟服务器发送求求
response=urllib.request.urlopen(request)
# 获取响应的数据
content = response.read().decode(utf8)
# print(content)
# post请求的参数必须编码:data = urllib.parse.urlencode(data)
# 编码之后 必须调用encode方法:data = urllib.parse.urlencode(data).encode(utf8)
# 参数是放在请求对象定制的方法中:request = urllib.request.Request(url=url,data=data,headers=headers)
# 字符串———》json对象
import json
obj = json.loads(content)
print(obj)
总结:post和get区别
以上是关于#yyds干货盘点#Python爬虫之Urllib用法合集的主要内容,如果未能解决你的问题,请参考以下文章
# yyds干货盘点 # Python网络爬虫之js逆向之远程调用(rpc)免去抠代码补环境简介
# yyds干货盘点 # Python网络爬虫之数美滑块的加密及轨迹~~动态js参数分析
#yyds干货盘点# Python - 爬虫_Ajax的请求方式