⽤python下载百度⽂库的代码
先去下载⼀个叫SWFToImage.dll的东西
再建⽴⼀个bat⽂件,并运⾏:
复制代码代码如下:
COPY SWFToImage.dll %windir%\system32
regsvr32 %windir%\system32\SWFToImage.dll
复制代码代码如下:
#⽤python下载百度⽂库的代码,需要的同志请修改,下⾯有提⽰
#wwwblogs/dearplain/
#code by plain
import urllib2
import win32com.client
import os
import sys
if __name__=='__main__':
#os.system('');
os.chdir('D:\my project\pywenku')#保存到哪个⽂件夹
SWFToImage=win32com.client.Dispatch("{479A1AAC-C148-40BB-9868-A9773DA66AF9}");
'''
allfile=os.listdir(".")
findrecord=0
for file in allfile:
if file==".record":
record=open(file,'rw')
findrecord=1
break
if findrecord==0:
record=open('.record','w')
'''
#url="wenku.baidu/view/8d3ed840be1e650e52ea9938.html?from=rec&pos=1&weight=2&lastweight=2&count=5" #url="wenku.baidu/view/f2fe7a3987c24028915fc37a.html?from=related&hasrec=1"
#url就是你要下载的⽂档的地址
url=sys.argv[1]
if url.find("")!=0:
print "error! the url is not correct"
print "downloading %s"%url
try:
urlReferer=url[url.index('http'):url.index('/v')]
print urlReferer
#urlbody=url[url.index('/v')-1:]
urlnum=url[url.index('ew/')+3:url.index('.htm')]
except ValueError:
print "parse url error"
#print urlnum
wenku='wenku.baidu'
reurl='/play/'
pagefrom='?pn='
downnum='&rn='
#try to get title and make dir
req=urllib2.Request(url)
学自学python要多久res=urllib2.urlopen(req)
ad()
try:
sfrom=data.index('<title>')+len('<title>')
素还真为什么不出场了#print sfrom
sbefore=sfrom+data[sfrom:].index('</title>')
#print sbefore
title=data[sfrom:sbefore]
title=title[:title.rindex('_')]
print 'downloading '+title
except ValueError:
print "get title error"
allfile=os.listdir(".")
if (title in allfile)==False:
极速浏览器进程快捷键os.mkdir(title)
os.chdir('./'+title)
#get the first swf
req=urllib2.Request('wenku.baidu'+reurl+urlnum+pagefrom+'1'+downnum+'1') req.add_header("Referer", urlReferer )
res=urllib2.urlopen(req)
ad()
res.close()
head=data[0:45]
pagenum=0
sfrom=head.index('\":\"')+len('\":\"')
sbefore=sfrom+head[sfrom:].index('\"')
pagenum=int(head[sfrom:sbefore])
print 'pagenum:'+str(pagenum)folder和file
if pagenum<=0 or pagenum>2000:
print "errorpagenum<0 or pagenum>2000"
data=data[106:]
swf=open("1.pywenku",'wb')
python基础代码大全下载swf.write(data)
swf.close()
i=1
SWFToImage.InputSWFFileName="%d.pywenku"%i
SWFToImage.ImageOutputType = 1
SWFToImage.ImageWidth=1048
SWFToImage.ImageHeight=1478
SWFToImage.Execute_Begin()
SWFToImage.FrameIndex = 1
SWFToImage.Execute_GetImage()
SWFToImage.SaveToFile("%d.jpg"%i)
SWFToImage.Execute_End()
allfile=os.listdir(".")
#从第⼆页下到最后⼀页
for i in range(2,pagenum+1):
if '%d.swf'%i in allfile:
continue
#not find in the dir mean
req=urllib2.Request('wenku.baidu'+reurl+urlnum+pagefrom+str(i)+downnum+'1') res=urllib2.urlopen(req)
ad()
data=data[106:]
swf=open("%d.pywenku"%i,'wb')
swf.write(data)
swf.close()
SWFToImage.InputSWFFileName="%d.pywenku"%i
SWFToImage.ImageOutputType = 1
SWFToImage.Execute_Begin()
SWFToImage.FrameIndex = 1
SWFToImage.Execute_GetImage() SWFToImage.SaveToFile("%d.jpg"%i) SWFToImage.Execute_End()
print 'task complete'
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论