爬虫----网络请求模块(urllib模块)

网络请求模块



urllib模块

python内置的网络请求模块(requests 是第三方的,是需要pip安装才能使用)

一、常用方法

● urllib.request.urlopen(“网址”) 作用 :向网站发起一个请求并获取响应
● 字节流 = response.read()
● 字符串 = response.read().decode(“utf-8”)
● urllib.request.Request"网址",headers=“字典”) urlopen()不支持重构User-Agent

二、响应对象

● read() 读取服务器响应的内容
● getcode() 返回HTTP的响应码
● geturl() 返回实际数据的URL(防止重定向问题)

三、urllib.request的使用

1、urllib.request.urlopen(‘网站’)

一旦碰到需要检查ua等等类的请求头里面的信息 这种方式就行不通
如何知道需要带上ua
一旦发现爬取下来的数据跟浏览器中看到的网页源代码不一样 就很可能被反爬

urllib.request.urlopen(“网址”) 作用 :向网站发起一个请求并获取响应

response = urllib.request.urlopen(url)

2、urllib.request.urlopen(‘请求对象’)

1、通过请求对象构造header
res = urllib.request.Rquest(url,headers=header)
在这个请求对象里面 不仅有url目标网址 还有请求头

2、发送请求 获取响应对象
response = urllib.request.urlopen(res)

3、在响应对象里 可以拿到响应状态码、响应内容等等

# response 响应对象里面的一些其他数据
# response.getheaders() 获取响应头
print(response.getheaders())
print(response.getheader('Bdqid'))

# 获取响应状态码
print(response.getcode())
print(response.status)

# 获取当前请求的url地址
print(response.geturl())

在响应对象里面需要重点关注的是 源代码(0)

result = response.read().decode('utf-8')

通过URLopen发送请求 获取响应对象

# 1、 创建一个请求对象 构造ua
res = urllib.request.Request(url, headers=header)
# 2、 通过URLopen发送请求 获取响应对象
response = urllib.request.urlopen(res)
print("响应对象的数据类型", type(response))  # 响应对象的数据类型 <class 'http.client.HTTPResponse'>
# 3、 从响应对象里面获取内容
result = response.read().decode('utf-8')
# print(result)

百度案例

import urllib.request
from http.client import HTTPResponse
# url 是目标网址
url = 'https://www.baidu.com/'
header = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36"
}
# response 是目标url地址发送请求后得到的响应对象
# response = urllib.request.urlopen(url, headers=header)
# TypeError: urlopen() got an unexpected keyword argument 'headers'
# 不能直接使用urlopen发送请求的时候 直接把headers传入

# .read()是用来读取响应对象里面的内容
# print(response.read()) #返回object
'''
通过打印响应内容 有两个问题:
1、数据不太对;
2、数据类型不太对 是字节流
'''

# 将数据转换为字符串
# print(type(response.read())) # <class 'bytes'>
# print(type(response.read().decode('utf-8')))# <class 'str'>
# print(response.read().decode('utf-8'))

# 1、 创建一个请求对象 构造ua
res = urllib.request.Request(url, headers=header)
# 2、 通过URLopen发送请求 获取响应对象
response = urllib.request.urlopen(res)
print("响应对象的数据类型", type(response))  # 响应对象的数据类型 <class 'http.client.HTTPResponse'>
# 3、 从响应对象里面获取内容
result = response.read().decode('utf-8')
# print(result)



# response 响应对象里面的一些其他数据
# response.getheaders() 获取响应头
print(response.getheaders())
print(response.getheader('Bdqid'))

# 获取响应状态码
print(response.getcode())
print(response.status)

# 获取当前请求的url地址
print(response.geturl())

四、urllib.parse的使用

一般用来处理带中文的url
使用urllib模块向一个携带中文url发送请求时报错:UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 41-42: ordinal not in range(128)

常用方法

● urlencode(字典)
● quote(字符串) (这个里面的参数是个字符串)

请求方式

● GET 特点
查询参数在URL地址中显示
● POST
1.在Request方法中添加data参数 urllib.request.Request(url,data=data,headers=headers)

2.data :表单数据以bytes类型提交,不能是str

1、字典格式的处理方式

# 字典格式
org = {"wd":"酷我"}
result = urllib.parse.urlencode(org)
# print(result)#wd=%E9%85%B7%E6%88%91
new_url = "https://www.baidu.com/s?ie=UTF-8&tn=62095104_35_oem_dg&" + result
# print(new_url)
# exit()

2、字符串格式的处理方式

# 字符串格式
string_org = "酷我"
string_result = urllib.parse.quote(string_org)
# print(string_result)
new_string_url = "https://www.baidu.com/s?ie=UTF-8&tn=62095104_35_oem_dg&wd=" + string_result
# print(new_string_url)
# exit()

酷我案例

import urllib.parse
import urllib.request
from http.client import HTTPResponse
# url 是目标网址
url = 'https://www.baidu.com/s?ie=UTF-8&tn=62095104_35_oem_dg&wd=酷我'#%E9%85%B7%E6%88%91
# 使用urllib模块向一个携带中文url发送请求时:UnicodeEncodeError: 'ascii' codec can't encode characters in position 41-42: ordinal not in range(128)
header = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36"
}

#通过urllib,parse处理url中的中文字样

# 字典格式
org = {"wd":"酷我"}
result = urllib.parse.urlencode(org)
# print(result)#wd=%E9%85%B7%E6%88%91
new_url = "https://www.baidu.com/s?ie=UTF-8&tn=62095104_35_oem_dg&" + result
# print(new_url)
# exit()

# 字符串格式
string_org = "酷我"
string_result = urllib.parse.quote(string_org)
# print(string_result)
new_string_url = "https://www.baidu.com/s?ie=UTF-8&tn=62095104_35_oem_dg&wd=" + string_result
# print(new_string_url)
# exit()

# 1、构造一个请求对象
res = urllib.request.Request(new_url,headers=header)
# 2、发送请求 获取响应
response = urllib.request.urlopen(res)
# 3、获取响应对象里面的内容
print(response.read().decode('utf-8'))

拓展

https://pvp.qq.com/web201605/wallpaper.shtml

# url是待处理的数据
url = 'https%3A%2F%2Fshp%2Eqpic%2Ecn%2Fishow%2F2735032810%2F1648434425%5F1265602313%5F29174%5FsProdImgNo%5F1%2Ejpg%2F200'
res = urllib.parse.unquote(url)
print(res)

拓展目的:要是以后在url中看到%+数字+英文字母的组合 而且 复制这个URL在新窗口中打不开的时候,就需要考虑urllib.parse.unquote(url)通过这种处理方式 看看你把你等到一个真正的URL(能不能在新窗口打开)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猩猩文学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值