一个有缺陷的下载图片代码

 1 import requests
 2 import re
 3 import os
 4 
 5 
 6 text=input("请输入要搜索的关键词:")
 7 if not os.path.exists(text):
 8     os.mkdir(text)
 9 # 1.确定网址
10 #url=("http://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&sf=1&fmq=&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&fm=index&pos=history&word=%s" %text)
11 
12 url ="http://image.baidu.com/search/index?ct=201326592%2C503316480&z=%2C&s=&tn=baiduimage&ipn=r&word="+text+"&pn=0&ie=utf-8&oe=utf-8&lm=-1&st=-1&fr=&se=&sme=&width=&height=&face=0&hd=1&latest=0&copyright=0"
13 # 2. 解析url 得到网页源代码
14 
15 r = requests.get(url)
16 
17 # ret = r.content #网页源代码 二进制数据
18 
19 ret =r.content.decode() #字符串网页源代码
20 
21 # 3.提取图片链接数据,必须是字符串类型的数据
22 # print (ret)
23 result = re.findall('"objURL":"(.*?)",',ret) # result 是一个列表类型
24 # print (result)
25 # 4.保存图片
26 for i in result:
27     print (i)
28     end = re.search('(jpg|png|gif|jpeg)$', i )
29     if end == None:
30         i=i+".jpg"
31     path = re.sub('/','',i[-10:])
32     try:
33         with open (text+"/%s" % path,"ab") as f:
34              r = requests.get( i,timeout=3)
35              f.write(r.content)
36     except Exception as e:
37         print (e)
38 
39 print (url)

还有几个问题没有解决:1.现在只能下载30张图片 2.还不能指定下载内容的父目录。

猜你喜欢

转载自www.cnblogs.com/bcyczhhb/p/10170801.html
今日推荐