当前位置: 移动技术网 > IT编程>脚本编程>Python > 爬取图片

爬取图片

2019年12月19日  | 移动技术网IT编程  | 我要评论

英文格言,驯养的近义词,割包皮一般要多少钱

from urllib.request import urlopen, urlretrieve
import re
# 大部分网址都可以直击换网址
url = "http://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=%e9%be%99%e5%8d%b7%e9%a3%8e"
html = urlopen(url)
obj = html.read().decode() # 得到网页html源码
print(obj)
urls = re.findall(r'"objurl":"(.*?)"', obj) # 在这一步,获取网页中的objurl部分,也就是真正的图片地址
index = 0
for u in urls:
if index <= 1: # 控制下载10张
try:
print('downloading...%d' % (index))
urlretrieve(u, 'pic' + str(index) + '.png') # urlretrieve函数 下载图片
index += 1
except exception: # 当由于网络原因或图片服务器出现问题时,捕获异常即可,不使程序退出
print('downloading failed%d' % (index))
finally:
print('downloading complete')
else:
break

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网