用python下载百度文库的代码

时间:2023-07-31 00:40:44 

先去下载一个叫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'
标签:python,百度文库
0
投稿

猜你喜欢

  • python3.X 抓取火车票信息【修正版】

    2022-01-26 01:24:53
  • asp显示字符串前5个文字

    2008-08-10 18:47:00
  • PyCharm第一次安装及使用教程

    2022-06-21 23:18:00
  • golang常用库之pkg/errors包第三方错误处理包案例详解

    2024-02-14 09:20:38
  • PyCharm永久激活方式(推荐)

    2023-06-12 00:53:33
  • 使用Requests库来进行爬虫的方式

    2022-07-20 18:00:33
  • python 实现dcmtk关联pacs功能推送下拉影像(推荐)

    2021-10-15 15:58:43
  • xmlHttp msxml3.dll 错误 '800c0008' 解决办法

    2008-08-31 20:44:00
  • Python+Matplotlib实现给图像添加文本标签与注释

    2022-05-02 12:40:16
  • CentOS7安装MySQL8的超级详细教程(无坑!)

    2024-01-25 13:25:41
  • Golang有类型常量和无类型常量的区别

    2024-02-01 17:42:23
  • 解决Pycharm中恢复被exclude的项目问题(pycharm source root)

    2023-10-30 11:10:44
  • Mysql误删数据解决方案及kill语句原理

    2024-01-26 20:22:15
  • mysql 5.7以上版本安装配置方法图文教程(mysql 5.7.12\\mysql 5.7.13\\mysql 5.7.14)

    2024-01-17 23:14:46
  • 推荐技术人员一款Python开源库(造数据神器)

    2023-11-10 19:57:45
  • Django3.2 自动发现所有路由代码详解

    2022-08-10 21:35:14
  • Oracle数据库的备份与恢复

    2024-01-21 02:13:58
  • uniapp页面跳转的五种方式总结

    2023-08-23 19:46:24
  • Python OpenCV基于霍夫圈变换算法检测图像中的圆形

    2023-11-12 05:33:36
  • ASP程序中使用断开的数据记录集的代码

    2012-12-04 20:20:28
  • asp之家 网络编程 m.aspxhome.com