python mobi_使用Python爬取mobi格式电纸书
#-*- coding: utf-8 -*-
importurllib2importreimportrequestsimportosimporthashlibdeffuckJS(js):importPyV8importre
#去掉
js=js[31:-9]for st in ['window','location',"'assign'","'href'","'replace'"]:
equal=re.findall('[_A-Za-z0-9 =]+%s;'%st,js)#找到变量赋值等式
if equal==[]:#有可能没有
continue
else:
equal=equal[0]
var=equal.split('=')[0].strip()#找出变量名
#把等式干掉
js=js.replace(equal,'')#把变量替换成它真正的意思
js=js.replace(var,st)#把['xx'] 替换成 .xx
js=js.replace("['%s']"%st.strip("'"),'.%s'%st.strip("'"))#将 window.href= 后的内容踢掉,因为当PyV8只输出最后一个等式的值
if re.findall('window\.href=.+',js)!=[]:
js=js.replace(re.findall('window\.href=.+',js)[0],'')#删掉location.xxx=
js=js.replace('location.href=','').replace('location.replace','').replace('location.assign','')#交给你了-v-
ctxt2 =PyV8.JSContext()
ctxt2.enter()#print ctxt2.eval(js)
trueAddr =ctxt2.eval(js)printtrueAddrreturntrueAddrdefdownloadMobi(name, url):#去掉windows下不合法的文件名
unlawName = '<>/\\|:""*?'
for i inunlawName:
name= name.replace(i, '')#正则表达式写的不够好导致的问题@@
if name.count(' img src=templateyeei_dream1cssyeeidigest_1.gif class=vm alt= title=') >0:
name= name.split(' ')[0]+'.mobi'
#避免重复下载
if os.path.exists('D:\Kindle114SpiderDownload\\' +name):print 'already have', namereturnurl= url.split(' ')[0]
s=requests.session()
username= '你的用户名'password= '你的密码'passwordMd5=hashlib.md5(password).hexdigest()
data= {'formhash': '23cd6c29', 'referer': '','username': username, 'password': passwordMd5, 'questionid':'0', 'answer':''}
res=s.post('http://www.kindle114.com/member.php?mod=logging&action=login&loginsubmit=yes&loginhash=LYn7n&inajax=1',data)#res = s.get('http://www.kindle114.com/forum.php?mod=attachment&aid=MTQ2NTB8ZjhkNjY3NmF8MTQxNjg5OTYxOXw0NDIxfDczNjI%3D')
try:
res= s.get(url, timeout = 200)except:print 'time out for', name#print 'content[:50]'
#print res.content[:50]
if res.content.count(') >0:print '!!!!!!!!!!!!!!!!!not a mobi, this file need gold coin!!!!!!!!!!!!!!!'
return
try:
with open('D:\\Kindle114SpiderDownload\\' + name, "wb") as code:
code.write(res.content)except:print '!!!!!!!!!!!!!!!!!!!!!遇到不合法文件名!!!!!!!!!!!!!!!!!!', namedefspiderThread(url, threadName):
req= urllib2.urlopen(url, timeout = 10)
text=req.read()if text.count(') ==0:
js=text
trueURL= 'http://www.kindle114.com/' +fuckJS(js)print 'trueURL', trueURL
req=urllib2.urlopen(trueURL)
text=req.read()#href = '(.*?)'
href = '(.*?)'href_re=re.compile(href)
href_info=href_re.findall(text)
bookSum=0for i inhref_info:if i[1].count('.mobi') >0:
bookSum+=1
if bookSum ==0:print '!!!bookSum = 0!!!!', text[:100]if bookSum == 1:print 'only one book in this thread'bookFileName= threadName + '.mobi'
for i inhref_info:if i[1].count('.mobi') >0:
link= i[0].replace('amp;','')break
printlink, bookFileName
downloadMobi(bookFileName, link)else:print str(bookSum), 'in this thread'
for i inhref_info:if i[1].count('.mobi') >0:
link= i[0].replace('amp;','')
bookFileName= i[1]printlink, bookFileName
downloadMobi(bookFileName, link)for pageNum in range(1, 125):
url= 'http://www.kindle114.com/forum.php?mod=forumdisplay&fid=2&filter=sortid&sortid=1&searchsort=1&geshi=1&page=' +str(pageNum)print '=============url', url,'==============='
try:
req= urllib2.urlopen(url, timeout = 10)except:print 'page time out', url
text=req.read()
href= '
(.*?)'href_re=re.compile(href)
href_info=href_re.findall(text)for i inhref_info:print i[0], i[1]
url= 'http://www.kindle114.com/'+i[0]
threadName= i[1]try:
spiderThread(url, threadName)exceptException , e:print '!!!!!!!!!!!!! Error with',threadName, url,'!!!!!!!!!!!!!!!!'
printe
raw_input('finish all!!!')
python mobi_使用Python爬取mobi格式电纸书相关推荐
- 使用Python爬取mobi格式电纸书
最近做了个微信推送kindle电子书的公众号:kindle免费书库 不过目前电子书不算非常多,所以需要使用爬虫来获取足够书籍. 于是,写了以下这个爬虫,来爬取kindle114的电子书. 值得注意的地 ...
- python用akshare批量爬取金融数据并保存为excel格式
python用akshare批量爬取数据并保存为excel格式 爬取的网站为http://vip.stock.finance.sina.com.cn/mkt/#hs_a的历史数据 首先,确定好你 下载 ...
- python爬取m3u8格式视频
python爬取m3u8格式视频 m3u8原理 脚本环境 爬取步骤 步骤的具体实现 python细节处理说明 参考代码 m3u8原理 当我们在网页播放视频时,网页向服务器发起一个以.m3u8结尾的连接 ...
- Python爬取m3u8格式的视频
声明:本文只作学习研究,禁止用于非法用途,否则后果自负,如有侵权,请告知删除,谢谢! Python爬取m3u8格式的视频目录 背景 1.文件信息 那什么是m3u8呢? 2.构造请求获得m3u8文件 3 ...
- 还在苦于Kindle的epub格式吗?python爬虫,一键爬取小说加txt转换epub。
还在苦于Kindle的epub格式吗?python爬虫,一键爬取小说加txt转换epub. 项目地址: https://github.com/Fruiticecake/dubuNovel/blob/m ...
- python 爬虫实例 电影-Python爬虫教程-17-ajax爬取实例(豆瓣电影)
Python爬虫教程-17-ajax爬取实例(豆瓣电影) ajax: 简单的说,就是一段js代码,通过这段代码,可以让页面发送异步的请求,或者向服务器发送一个东西,即和服务器进行交互 对于ajax: ...
- Python学习笔记:爬取网页图片
Python学习笔记:爬取网页图片 上次我们利用requests与BeautifulSoup爬取了豆瓣<下町火箭>短评,这次我们来学习爬取网页图片. 比如想爬取下面这张网页的所有图片.网址 ...
- python实战1.0——爬取知乎某问题下的回复
python实战1.0--爬取知乎某问题下的回复 确定问题 爬取 进行简单筛选 保存数据 # 获取问题下的回复总数 def get_number():url = 'https://www.zhihu. ...
- 利用python的scrapy框架爬取google搜索结果页面内容
scrapy google search 实验目的 爬虫实习的项目1,利用python的scrapy框架爬取google搜索结果页面内容. https://github.com/1012598167/ ...
- Python爬虫---影评的爬取
Python爬虫-影评的爬取 介绍:爬虫练手,使用Requests库进行豆瓣影评的爬虫,做成词云图,写文章主要做一些问题解决的记录. 运行环境:python 3.8, Pycharm 关于在豆瓣爬取影 ...
最新文章
- 配置CACTI监控MySQL数据库状态(1)准备工作
- Algs4-2.2.29自然的归并排序(未解决)
- JS三教九流系列-require.js-网站模块化开发
- ​Spring事务的传播行为案例分析
- letecode [160] - Intersection of Two Linked Lists
- pc receiver2.0.3_【工控笔记】WINCC 7.4与博途V15.1仿真软件S7PLCSIM V15 通信
- Web安全手册(漏洞理解、漏洞利用总结)
- 工作两个月的感受随笔
- 5.1.8 DELETE删除数据
- 我购买了一台acer笔记本
- j结对开发——石家庄地铁查询系统
- python判断文件格式_Python判断上传文件类型
- 十次方项目开发系列【3】:基于MyBatis Plus实现条件查询和分页
- android ios 逆向工程,iOS逆向工程之砸壳
- Mock实现单元测试报错:Argument passed to when( ) is not a mock!
- OS-S1 操作系统概论
- 手机浏览器简单搜索ua标识
- ffmpeg的中文文档(二)
- 艾永亮:打造超级产品做到这五点,有效提高用户对企业的印象
- Motrix—完美替代迅雷的全能下载器
热门文章
- 记sublime手动下载安装emmet和PyV8文件记录
- emmet插件及PyV8
- 仿淘宝中心轮播图 JS[代码+详细讲解+效果图]
- FTP超详解及搭建步骤
- jdk版本和java版本一样吗_jdk版本查看,以及java -version 和JAVA_HOME不一致问题
- 3dmax渲染出图噪点原因及解决方法
- 等宽字体 Monospaced Font
- Java中日期格式化yyyyMMdd和YYYYMMdd的区别
- Pytorch:卷积神经网络-空洞卷积
- access数据库应用系统客观题_Access制作客观题考试系统的应用