爬虫----request简介(以及urllib模块和request模块保存图片区别)


request模块

一、安装

● pip install requests
● 在开发工具中安装

二、request常用方法

● requests.get(网址)

三、响应对象response的方法

● response.text 返回unicode格式的数据(str)
● response.content 返回字节流数据(二进制)
● response.content.decode(‘utf-8’) 手动进行解码
● response.url 返回url
● response.encode() = ‘编码’

保存图片案例

request模块 保存图片

首先导入request模块,然后向url发送get请求

import requests

url = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_match%2F0%2F11012685795%2F0.jpg&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651324072&t=b712c65bdf48d9979a572572a72b7d4b"
img_name = 'code.png'
# 发送请求
res = requests.get(url)
print(res.content)

request模块 第一种保存图片的方式

通过with open来将图片保存

# 保存图片、音频之类会使用wb-->以二进制的方式写入
with open(img_name, 'wb') as file_obj:
    # 保存图片、音频之类 会使用content-->以二进制写入去响应对象里面取
    file_obj.write(res.content)

request模块 第二种保存图片的方式

通过open来将图片保存

# file_obj是一个文件对象
file_obj = open(img_name,'wb')
file_obj.write(res.content)
# 没用with 要手动关闭
file_obj.close()

urllib模块 保存图片

首先导入urllib模块

import urllib.request

url = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_match%2F0%2F11012685795%2F0.jpg&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651324072&t=b712c65bdf48d9979a572572a72b7d4b"
img_name = 'code3.png'

第一种导入方式,以及第一种保存图片方式

可以通过书写指定地址将图片保存

file_name = r'F:\pycharm program\pythonProject\爬虫\code4.png'
urllib.request.urlretrieve(url,file_name)

第二种导入方式,以及第二种保存图片方式

通过直接给图片命名,保存在当前文件目录下

import urllib.request
urllib.request.urlretrieve(url,img_name)

完整代码如下:

import requests
import urllib.request

url = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_match%2F0%2F11012685795%2F0.jpg&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651324072&t=b712c65bdf48d9979a572572a72b7d4b"
img_name = 'code3.png'
# 发送请求
# res = requests.get(url)
# print(res.content)
# # request模块 第一种保存图片的方式
# # 保存图片、音频之类会使用wb-->以二进制的方式写入
# with open(img_name, 'wb') as file_obj:
#     # 保存图片、音频之类 会使用content-->以二进制写入去响应对象里面取
#     file_obj.write(res.content)

# request模块 第二种保存图片的方式
# # file_obj是一个文件对象
# file_obj = open(img_name,'wb')
# file_obj.write(res.content)
# # 没用with 要手动关闭
# file_obj.close()



#urllib模块
'''
url 是图片的url地址
img_name 是图片的保存的名字
'''
# 第一种导入方式,以及第一种保存图片方式
from urllib import request
file_name = r'F:\pycharm program\pythonProject\爬虫\code4.png'
urllib.request.urlretrieve(url,file_name)
# 第二种导入方式,以及第二种保存图片方式
import urllib.request
urllib.request.urlretrieve(url,img_name)
  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

猩猩文学

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

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

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

打赏作者

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

抵扣说明:

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

余额充值