多线程下载 se 图
#!/usr/bin/env python
#-*-coding:UTF-8-*-
import urllib,sys,time,threading,random
class o_thread(threading.Thread):
def __init__(self):
self.imgurl=""
threading.Thread.__init__(self)
def run(self):
if not self.imgurl:return
try:
f=urllib.urlopen(self.imgurl,"r")
data=f.read()
except:
return
if len(data)<55000:
f.close()
return
if data.find("html")!=-1 or data.find("HTML")!=-1:
f.close()
return
newfile="/home/manman/images/%s%s.jpg"%(time.time(),str(random.randrange(1,100))) #存放图片的目录,此处应自行修改
f=open(newfile,"w")
f.write(data)
f.close()
print "ok"
def manman(url):
try:
f=urllib.urlopen(url,"r")
data=f.read()
except:
print "da bu kai"
return
data=data.replace("\'","\"")
ls=[]
if not data:return
start=0
end=0
while True:
start=data.find("<img src=\"",end)
if start==-1:break
start=start+10
end=data.find("\"",start)
if end==-1:break
man=data[start:end]
if man.find("http")==-1:continue
ls.append(man) #找到一个图片路径
for i in ls:
try:
one=o_thread()
one.imgurl=i
one.start()
except:
continue
url="http://70.87.129.46/read.php?tid="
for i in range(30200,80000):
newurl=url+str(i)
manman(newurl)
print i