python为在线漫画网站自制非官方API(未完待续)

来源:互联网 发布:机器人走迷宫编程 编辑:程序博客网 时间:2024/06/08 09:03

接下来将记录我一步一步写一个非官方API的过程,因为一些条件的约束,最后的成品可能很粗暴简陋

现在介绍要准备的所有工具:

系统:ubuntu 14.04

语言:python 2.7

需要自行安装的库:flask,BeautifulSoup4,requests,selenium,pinyin,phantomjs-1.9.8

服务器:Sina App Engine

因为成本原因我选择了Sina App Engine,因为免费,但是免费也带来了一定的麻烦就是功能不全,虽然Sina App Engine允许安装python的第三方库,但是对于javascript解释器我很无奈,如果可以我很希望Sina App Engine有nodejs的运行环境,这样就会方便很多。

当然我写的只是简单实现,并没有考虑到效率和优化

下面是几个重要的文档:

http://beautifulsoup.readthedocs.org/zh_CN/latest/

http://cn.python-requests.org/zh_CN/latest/

http://dormousehole.readthedocs.org/en/latest/

http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html

https://github.com/cleverdeng/pinyin.py

(一定要多看文档,一定要多看文档,一定要多看文档,重要的事要说三遍,因为我经常不看文档)

下面是搜索漫画API开始:

分析:

我选择的网站是:http://cnc.dm5.com(对于网站我只想说非常抱歉)
为什么选择这个网站,因为是我最常逛的在线漫画网站,仅此而已
接下来我们看一下它的搜索页面,当你在首页输入书名,点击搜索以后,URL会跳转
举个例子:
搜索“一拳超人”
结果页面如下:
http://www.dm5.com/search?title=一拳超人&language=1

这样我们就知道搜索结果页面,你可以手动将 一拳超人 改成你喜欢的漫画名,看到会不会跳转到那本漫画的搜索结果页上
但是结果页上内容太多了,并不只有我要的那本漫画,于是我又做了一件事,将中文改成pinyin

http://www.dm5.com/search?title=yiquanchaoren&language=1

结果页面上只有我需要的漫画,现在发现了拼音搜索会更精确
当我们知道这样就可以进入搜索页,那我们就可以分析这个页面,找到我们需要的信息
那么我们需要什么呢?
任何浏览器的F12都是强大的,在这个页面上按下F12,就是新世界的大门,哈哈哈......
定位元素,发现需要的信息是在 class="ssnrk" 中(友情提示,F12下的源码是解析JS以后的,想知道现实情况最后先输出在一个文件上看看)
#coding:utf-8import requestswfile = open('url.html','w')r = requests.get('http://cnc.dm5.com/search?title=yiquanchaoren').contentwfile.write(r)
这是最简单的测试方法,将源码写进一个文件中,然后去文件中找有没有 class="ssnrk" ,如果存在以后就以文件的元素结构分析(以后的每一个页面最好都做一下这步)
现在发现文件中也有这个元素,OK,我们需要什么呢?漫画名和它的地址,当然你要图片也可以
我们发现class="ssnrk"元素下<div class="ssnr_yt"><dl><a href="/manhua-yiquanchaoren/" title="一拳超人"><img height="127" width="95" src="http://mhfm5.tel.cdndm5.com/11/10684/20150430155820_130x174_13.jpg" /></a></dl>里面包含了我们要的一切,名称,链接,图片
那么我们怎么得到这些有效信息呢?

#coding:UTF-8import urllib2from bs4 import BeautifulSoupfrom bs4 import UnicodeDammitimport requestsimport reimport json#搜索结果页面URLurl='http://cnc.dm5.com/search?title=guanlangaoshou'r = requests.get(url).contentsoup = BeautifulSoup(r)#找出所有class="ssnr_yt"元素,然后循环找出下面的a元素,放进找一个list中ssjg_list=[]for line in soup.find_all(class_="ssnr_yt"):    ssjg_list.append(line.find('a'))#循环将所有图片,URL,名称放进字典中z_url="http://cnc.dm5.com"#总的字典json_ss = {}#结果数量json_ss['num']=len(ssjg_list)#计数器j_s=0for line in ssjg_list:    j_s=j_s+1    #单个字典    json_s={}    soup_a = BeautifulSoup(str(line))    json_s['title']=soup_a.a.get('title')#找出a元素title属性的值    json_s['url']=z_url+soup_a.a.get('href')#找出a元素hresf属性的值    json_s['img']=soup_a.a.img.get('src')#找出a元素下img元素src属性的值    json_ss[j_s]=json_s#以json形式输出print json.dumps(json_ss,ensure_ascii=False,indent=2)

为什么这里搜索内容变成guanlangaoshou呢?因为有些漫画真的有多个结果,比如这个guanlangaoshou ,就有四个下面是得到的结果:
{  "3": {    "url": "http://cnc.dm5.com/manhua-guanlangaoshou/",     "img": "http://mhfm9.tel.cdndm5.com/1/860/860_c.jpg",     "title": "灌篮高手"  },   "1": {    "url": "http://cnc.dm5.com/manhua-guanlangaoshoujuchangban/",     "img": "http://mhfm5.tel.cdndm5.com/1/380/380_c.jpg",     "title": "灌篮高手剧场版"  },   "2": {    "url": "http://cnc.dm5.com/manhua-guanlangaoshoushirihou/",     "img": "http://mhfm8.tel.cdndm5.com/7/6270/6270_c.jpg",     "title": "灌篮高手十日后"  },   "num": 4,   "4": {    "url": "http://cnc.dm5.com/manhua-guanlangaoshouquanguodasaipian-quancai/",     "img": "http://mhfm1.tel.cdndm5.com/8/7312/20150526100857_130x174_12.jpg",     "title": "灌篮高手全国大赛篇(全彩)"  }}

现在写的都是小零件和一步一步测试,等到最后会组装起来搜索结果先告一段落,现在已搜索到的结果为前提,进行下一步,找出这本漫画所有的章节的URL:这次以 yiquanchaoren 为例:URL:http://cnc.dm5.com/manhua-yiquanchaoren/这是漫画的章节页,目前是74话(挺少的,看着不过瘾)继续使用F12这个武器,会发现章节的URL都在class=nr6 lan2下的a中:
#coding:UTF-8import urllib2from bs4 import BeautifulSoupfrom bs4 import UnicodeDammitimport requestsimport reimport jsonurl='http://cnc.dm5.com/manhua-yiquanchaoren/'r = requests.get(url).contentsoup = BeautifulSoup(r)#找出所有class="nr6 lan2"下所有aclass_a = soup.find_all(class_="nr6 lan2")s = BeautifulSoup(str(class_a)).find_all('a')#这个list用来存放所有章节URLurl_list=[]z_url = 'http://cnc.dm5.com'#循环所有a,获取URLfor line in s:    bs_a = BeautifulSoup(str(line))    #这个判断是用来去除不需要的URL,它们是已http://开头,正确的URL是/开头    if not bs_a.a.get('href').split('/')[0]:        w_url = z_url+bs_a.a.get('href')        url_list.append(z_url+bs_a.a.get('href'))#去重排序URLprint sorted(list(set(url_list)),key=str.lower)

这样就得到了所有章节的URL,现在这里先放一下,我们点击进入一个章节,看一会儿漫画,诶,居然有广告,开始找出需要的漫画url,按下F12,我们可以马上找到,真的找到了吗?
之前说过浏览器下的源码是经过js解析以后的完整源代码,而python不具备js的解析,很简单的实验,之前也提到过,将获取的源码写入一个文件中
#coding:utf-8import requestswfile = open('url.html','w')r = requests.get('http://cnc.dm5.com/m208526/').contentwfile.write(r)
在这个url.html中搜索id="cp_img",原本下面应该有的img元素不见了,整个id下面竟然没有子节点,再往下看,会找到关于img的js函数,现在可以确定,图片是由js控制的,python不能解析的话,只能依赖别的工具selenium,phantomjs-1.9.8,好复杂的说
话不多说,直接上代码
#coding:UTF-8import urllib2from bs4 import BeautifulSoupfrom bs4 import UnicodeDammitimport requestsimport reimport jsonfrom selenium import webdriver  import sys#这两个url,待会儿再解释#url="http://cnc.dm5.com/m208526-p1"url="http://cnc.dm5.com/m208526/#ipg1"#一个是windows下的路径,一个是ubuntu下的路径,根据自己的安装路径#driver = webdriver.PhantomJS(executable_path='C:\\YXJR_ZJL\\RuanJian\\phantomjs-2.0.0-windows\\bin\\phantomjs') driver = webdriver.PhantomJS(executable_path='/home/zjl//phantomjs-1.9.8/bin/phantomjs')driver.get(url)#根据xpath找到id="cp_image"的img元素,获取src属性值r= driver.find_element_by_xpath("//img[@id='cp_image']").get_attribute('src')#下载图片,保存本地with open(r'yy11.jpg','wb') as f:    f.write(requests.get(r).content)

上面是两个url均有效,当你点击下一页的时候url后面会出现一个页数的参数,默认第一页是没有的就算加上参数也不影响将图片保存本地看一下,会发现一个问题,为什么下载的图片和现实看到的图片不一样呢?下载下来的图片是提示要从正确的网站进入,难道是传说中的防盗链?于是我把代码改了一下:
#coding:UTF-8import urllib2from bs4 import BeautifulSoupfrom bs4 import UnicodeDammitimport requestsimport reimport jsonfrom selenium import webdriver  import sys#这两个url,待会儿再解释#url="http://cnc.dm5.com/m208526-p1"url="http://cnc.dm5.com/m208526/#ipg1"#一个是windows下的路径,一个是ubuntu下的路径,根据自己的安装路径#driver = webdriver.PhantomJS(executable_path='C:\\YXJR_ZJL\\RuanJian\\phantomjs-2.0.0-windows\\bin\\phantomjs') driver = webdriver.PhantomJS(executable_path='/home/zjl//phantomjs-1.9.8/bin/phantomjs')driver.get(url)#根据xpath找到id="cp_image"的img元素,获取src属性值r= driver.find_element_by_xpath("//img[@id='cp_image']").get_attribute('src')#获取图片的头地址rr = r.split('//')[1].split('/')[0]#自定义头信息headers = {'Host':rr,'Referer':url,'User-Agent':'Mozilla/5.0 (Windows NT 6.1; rv:38.0) Gecko/20100101 Firefox/38.0'}imgUrl = r#下载图片with open(r'yy11.jpg','wb') as f:    #访问时发送自定义的头信息    f.write(requests.get(imgUrl,headers=headers).content)
现在发现ok了,下载的图片正常显示
到这里,用上面所有的知识已经可以写一个漫画下载器了,可以去网站下载漫画看了,哈哈哈......

今天先到这里,下次继续










0 0
原创粉丝点击