CSDN也来了快半年了,作为一个python小白,把我最近学的代码展现展现,大家帮忙看看有没有毛病
#!usr/bin/env python
#_*_ coding:utf-8 _*_
'''
@Author:薏苡仁
@WeChat official Account(微信公众号):python学者
@file:百度图片.py
@Time:2020/2/13 8:33
motto:不积硅步无以至千里,不积小流无以成江河,程序人生的精彩需要坚持不懈地积累!
'''
import requests
import re
import os
url = input("请输入百度图片首页地址:")
html = requests.get(url).text
tu_url = re.findall(r'"objURL":"(.*?)"', html) #一个图片对应许多链接,经测试图片链接在 objURL 这个标签内。用正则找出
dirpath = os.getcwd() + '\\pictures'
if not os.path.isdir(dirpath):
os.mkdir(dirpath)
index = 1
for tu_url in tu_urls:
print("Downloading:", tu_url )
try:
res = requests.get(tu_url )
if str(res.status_code)[0] == "4":
print("未下载成功:",tu_url )
Process finished with exit code 0
except Exception as e:
print("未下载成功:", tu_url )
filename = os.path.join(dirpath, str(index) + ".jpg")
with open(filename, 'wb') as f:
f.write(res.content)
num += 1
print("下载结束,一共 {}张图片" .format(num))
感谢大家指导
附上运行后的结果
C:\Users\15237\AppData\Local\Programs\Python\Python37\python.exe "G:/Pycharm 文档/百度图片.py"
Downloading: http://img01.taopic.com/170909/240478-1FZZA05449.jpg
Downloading: http://hbimg.b0.upaiyun.com/d21ff50ec5bb71788cac7db06a74f253075580c31262c-9GEqXk_fw658
Downloading: http://img.jdzj.com/userdocument/2012y/yldsfk/picture/201292911725.jpg
Downloading: http://pic.feizl.com/upload/allimg/170614/0T24025A-0.jpg
Downloading: http://attachments.gfan.com/forum/attachments2/day_111113/1111131811fe8b6d1575c620d7.jpg
Downloading: http://pic1.win4000.com/pic/5/9e/4933293953.jpg
Downloading: http://pic.jj20.com/up/allimg/911/011Q6153015/16011Q53015-1.jpg
Downloading: http://2c.zol-img.com.cn/product/54/182/ce8BU68OqwLQ.jpg
Downloading: http://pic.feizl.com/upload/allimg/170801/735u2jso3e3p5.jpg
Downloading: http://img07.mifile.cn/v1/MI_55950AFBBEDCB/T10dKTBTYT1RXrhCrK.jpg
Downloading: http://image3.xyzs.com/upload/8e/82/830/20150505/143078164393461_0.jpg
Downloading: http://img.pconline.com.cn/images/upload/upc/tx/softbbs/1103/09/c0/6952476_1299664187183_1024x1024soft.jpg
Downloading: http://pic.t139.com/chemo/201412/11/s_548962fe34f64.jpg
Downloading: http://hbimg.b0.upaiyun.com/b30c7723c9a6b0bd3d8a6ea9ef26fd9d1a326f6e1693f-kv7UtV_fw658
Downloading: http://img.pconline.com.cn/images/upload/upc/tx/wallpaper/1208/14/c0/12901419_1344914249805.jpg
Downloading: http://img.pconline.com.cn/images/upload/upc/tx/softbbs/1109/07/c0/8900901_1315408796310_1024x1024soft.jpg
Downloading: http://i1.shouyou.itc.cn/2015/news/2015/05/29/1280x720bz0530_12.jpg
Downloading: http://pic1.win4000.com/mobile/0/53101b05cfecb.jpg
Downloading: http://a.vpimg3.com/upload/merchandise/353690/DUSHI-063804-0021-4.jpg
Downloading: http://01.minipic.eastday.com/20161213/20161213135128_5a29f92732d13a0461e09c15250d4cd5_1.jpeg
Downloading: http://image.hnol.net/c/2011-04/09/19/201104091935229941-993477.jpg
Downloading: http://i9.download.fd.pchome.net/t_960x600/g1/M00/08/14/oYYBAFOW1TSIUbBvAAkoYZLkEgEAABl5AB0Ff0ACSh5667.jpg
Downloading: http://tupian.enterdesk.com/2012/1126/czs/02/xinggan%20%282%29.jpg
Downloading: http://img.pconline.com.cn/images/upload/upc/tx/photoblog/1511/15/c4/15264222_15264222_1447554104854.jpg
Downloading: http://00.minipic.eastday.com/20170414/20170414154348_f4d91e0052627021c5229e5b6f504c51_1.jpeg
Downloading: http://img2.vipshop.com/upload/merchandise/36136/yileisi-8003-036-1.jpg
Downloading: http://pic.feizl.com/upload/allimg/170615/1201131226-3.jpg
Downloading: http://img.pconline.com.cn/images/upload/upc/tx/itbbs/1109/14/c2/8957867_1315979581611_1024x1024it.jpg
Downloading: http://attach.bbs.miui.com/forum/201212/07/142638xznzyenl3chbbc83.jpg
Downloading: http://www.zhlzw.com/sj/UploadFiles_9645/201108/20110828145953141.jpg
下载结束,一共 31张图片
Process finished with exit code 0