python 爬取图片

python 爬取图片,第1张

概述获得图片链接,网上的图片都有唯一的url import urllib.requestimage_url=‘https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1569075968903&di=6e275342eb912831affe1c2f5511e05d&imgtype=0&src=http%3A%2F%2F 获得图片链接,网上的图片都有唯一的url
import urllib.requestimage_url=https://timgsa.baIDu.com/timg?image&quality=80&size=b9999_10000&sec=1569075968903&di=6e275342eb912831affe1c2f5511e05d&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F14c272157c7345f33bab613327d7ee11a8b2c5344ea1a-rSck09_fw658# 方法一:urllib.request.urlretrIEve(image_url,chun.jpg)# 方法二:response=urllib.request.urlopen(image_url)file=open(rE:\qing.jpg,wb)# 二进制格式,wb二进制格式写入file.write(response.read())file.close()
#方法三:with open(
qing.jpg,wb) as fp: fp.write(response.read())

 

熟悉urllib的各类函数的使用
import urllib.requesturl=http://www.baIDu.comresponse=urllib.request.urlopen(url=url)print(response.readlines())print(dict(response.getheaders()))print(response.read().decode())with open(baIDu.HTML,w,enCoding=utf8)as fp:    fp.write(response.read().decode())

 

图片获取步骤: 得到url response=urllib.request.urlopen(url)打开获得的url response.read().decode()# decode()将读出的信息以二进制字节形式打开, 将获得的文件输出,有方法一,方法二、方法三可以直接进行传数 方法一:
with open(rE:\qing.jpg,wb)  as fp:# wb以二进制字节进行读写    fp.write(response.read())

 

方法二:
file=open(rE:\qing.jpg,wb)file.write(url)file.close()
总结

以上是内存溢出为你收集整理的python 爬取图片全部内容,希望文章能够帮你解决python 爬取图片所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1191211.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-03
下一篇2022-06-03

发表评论

登录后才能评论

评论列表(0条)

    保存