用python下载百度文库的代码
程序员文章站
2022-07-11 15:03:52
先去下载一个叫swftoimage.dll的东西 再建立一个bat文件,并运行: 复制代码 代码如下: copy swftoimage.dll %windir%\syste...
先去下载一个叫swftoimage.dll的东西
再建立一个bat文件,并运行:
copy swftoimage.dll %windir%\system32
regsvr32 %windir%\system32\swftoimage.dll
#用python下载百度文库的代码,需要的同志请修改,下面有提示
#http://www.cnblogs.com/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="http://wenku.baidu.com/view/8d3ed840be1e650e52ea9938.html?from=rec&pos=1&weight=2&lastweight=2&count=5"
#url="http://wenku.baidu.com/view/f2fe7a3987c24028915fc37a.html?from=related&hasrec=1"
#url就是你要下载的文档的地址
url=sys.argv[1]
if url.find("http://")!=0:
print "error! the url is not correct"
sys.exit()
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"
sys.exit()
#print urlnum
wenku='wenku.baidu.com'
reurl='/play/'
pagefrom='?pn='
downnum='&rn='
#try to get title and make dir
req=urllib2.request(url)
res=urllib2.urlopen(req)
data=res.read()
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"
sys.exit()
allfile=os.listdir(".")
if (title in allfile)==false:
os.mkdir(title)
os.chdir('./'+title)
#get the first swf
req=urllib2.request('http://wenku.baidu.com'+reurl+urlnum+pagefrom+'1'+downnum+'1')
req.add_header("referer", urlreferer )
res=urllib2.urlopen(req)
data=res.read()
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)
if pagenum<=0 or pagenum>2000:
print "error!!!pagenum<0 or pagenum>2000"
sys.exit()
data=data[106:]
swf=open("1.pywenku",'wb')
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()
os.rename("%d.pywenku"%i,"%d.swf"%i)
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('http://wenku.baidu.com'+reurl+urlnum+pagefrom+str(i)+downnum+'1')
res=urllib2.urlopen(req)
data=res.read()
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()
os.rename("%d.pywenku"%i,"%d.swf"%i)
res.close()
print 'task complete'
再建立一个bat文件,并运行:
复制代码 代码如下:
copy swftoimage.dll %windir%\system32
regsvr32 %windir%\system32\swftoimage.dll
复制代码 代码如下:
#用python下载百度文库的代码,需要的同志请修改,下面有提示
#http://www.cnblogs.com/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="http://wenku.baidu.com/view/8d3ed840be1e650e52ea9938.html?from=rec&pos=1&weight=2&lastweight=2&count=5"
#url="http://wenku.baidu.com/view/f2fe7a3987c24028915fc37a.html?from=related&hasrec=1"
#url就是你要下载的文档的地址
url=sys.argv[1]
if url.find("http://")!=0:
print "error! the url is not correct"
sys.exit()
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"
sys.exit()
#print urlnum
wenku='wenku.baidu.com'
reurl='/play/'
pagefrom='?pn='
downnum='&rn='
#try to get title and make dir
req=urllib2.request(url)
res=urllib2.urlopen(req)
data=res.read()
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"
sys.exit()
allfile=os.listdir(".")
if (title in allfile)==false:
os.mkdir(title)
os.chdir('./'+title)
#get the first swf
req=urllib2.request('http://wenku.baidu.com'+reurl+urlnum+pagefrom+'1'+downnum+'1')
req.add_header("referer", urlreferer )
res=urllib2.urlopen(req)
data=res.read()
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)
if pagenum<=0 or pagenum>2000:
print "error!!!pagenum<0 or pagenum>2000"
sys.exit()
data=data[106:]
swf=open("1.pywenku",'wb')
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()
os.rename("%d.pywenku"%i,"%d.swf"%i)
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('http://wenku.baidu.com'+reurl+urlnum+pagefrom+str(i)+downnum+'1')
res=urllib2.urlopen(req)
data=res.read()
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()
os.rename("%d.pywenku"%i,"%d.swf"%i)
res.close()
print 'task complete'
上一篇: perl 删除数组元素的几种方法小结
下一篇: 有关perl的内置特殊变量介绍