抓取漫画的网址是:sf互动传媒

抓取漫画的由来也是看了知乎上有人说用爬取漫画,然后自己也玩玩

首页中每个漫画的url是类似这样存储的:

<tr>
<td height="30" align="center" bgcolor="#FFFFFF">
<a href="http://comic.sfacg.com/HTML/KOL/" target="_blank">K.O.I 偶像之王</a>
</td>
</tr>

然后用lxml通过cssselect(tr>td>a)将能用到的解析出来,然后解析出来会有很多其他的页面的url和信息,然后我是通过url中包含"/mh/"或者是"/HTML/"进行过滤的

比较蠢的办法了

然后通过对象,将过滤出来的漫画的url和漫画的名字保存在一个这样的类中,然后通过列表进行存储

class Cartoon():
url = None
name = None

然后用随便一个漫画作为例子:勇者赫鲁库

漫画一共很多章,并且所有章的信息都在如下标签中包含

<ul class="serialise_list Blue_link2">....</ul>

然后通过BS将每一章的信息进行存储,然后可以看到其完整的url是:http://comic.sfacg.com/HTML/YZHLK/096/

然后每一章会有很多的page,并且每一章的内容是ajax进行加载的,然后从检查->网络中可以看到有一个这样的请求

然后请求的response里面包含本章的所有图片,然后只需将在每一章的页面中将.js接口找到,然后将本章的图片信息存储,然后就可以得到本章的图片信息,然后存储到本地

# -*- coding: utf-8 -*-
import re
import urllib
import urllib2
import os
import stat
import itertools
import re
import sys
import requests
import json
import time
import socket
import urlparse
import csv
import random
from datetime import datetime, timedelta
import lxml.html from zipfile import ZipFile
from StringIO import StringIO
from downloader import Downloader
from bs4 import BeautifulSoup
from HTMLParser import HTMLParser
from itertools import product
import sys
reload(sys)
sys.setdefaultencoding('utf8')
URL = 'http://comic.sfacg.com'
picture = 'http://coldpic.sfacg.com' class Cartoon():
url = None
name = None def download(url, user_agent='wswp', num_try=2): headers = {'User_agent': user_agent}
request = urllib2.Request(url, headers=headers)
try:
html = urllib2.urlopen(request).read()
except urllib2.URLError as e:
print 'Download error', e.reason
html = None
if num_try > 0:
if hasattr(e, 'code') and 500 <= e.code < 600:
return download(url, user_agent, num_try - 1)
elif e.code == 403:
return None
return html def get_section_url(url):
html = download(url)
if html == None:
return None
soup = BeautifulSoup(html, "html.parser")
results = soup.find_all(name='ul', attrs={'class': 'serialise_list Blue_link2'})
res = r'<a.*?href="([^"]*)".*?>([\S\s]*?)</a>'
links = re.findall(res, str(results),re.S | re.M)
return links def get_section_page(url): html = download(url)
if html == None:
return None
soup = BeautifulSoup(html, "html.parser")
results = soup.find_all(name='script', attrs={'type': 'text/javascript'})
tt = len(results)
js = results[tt-1]
mm = js.get('src')
if mm == None:
result = soup.find_all(name='script', attrs={'language': 'javascript'})
js1 = result[1]
mm = js1.get('src')
html1 = download(URL+mm)
list = html1.split(';')
List = []
for each in list:
if 'picAy[' in each:
src = each.split('=')
List.append(picture+src[1][2:-1]) return List def download_cartoon(url, cartoon_name,Section,num): path = "自己定义的路径"+cartoon_name if not os.path.exists(path):
os.mkdir(path)
path = path + "/"+Section
if not os.path.exists(path):
os.mkdir(path)
content = requests.get(url).content
with open(path + '/' + str(num) + '.jpg', 'wb') as f:
f.write(content)
print "Downloading cartoon_name " + path + str(num)+ "下载完成"
f.close() if __name__ == '__main__':
cartoon_list = [] html = download(URL)
tree = lxml.html.fromstring(html)
results = tree.cssselect('tr > td > a')
for each in results:
ti = each.get('href')
if '/mh/' in ti or '/HTML/' in ti:
if each.text_content() != "":
cartoon = Cartoon()
cartoon.url = each.get('href')
cartoon.name = each.text_content().replace(' ','')
cartoon_list.append(cartoon) for each in cartoon_list:
print each.url
print each.name
links = get_section_url(each.url)
links = list(reversed(links))
section = 0
for link in links:
ul = URL + link[0]
List = []
List = get_section_page(ul)
section = section + 1
Section = r'第'+ str(section) + r'章'
num = 1
for mm in List:
#print mm
download_cartoon(mm,each.name,Section,num)
num = num + 1
print each.name + Section + "下载完成"+str(num-1)+"张"

python爬取漫画的更多相关文章

  1. Python 爬取所有51VOA网站的Learn a words文本及mp3音频

    Python 爬取所有51VOA网站的Learn a words文本及mp3音频 #!/usr/bin/env python # -*- coding: utf-8 -*- #Python 爬取所有5 ...

  2. python爬取网站数据

    开学前接了一个任务,内容是从网上爬取特定属性的数据.正好之前学了python,练练手. 编码问题 因为涉及到中文,所以必然地涉及到了编码的问题,这一次借这个机会算是彻底搞清楚了. 问题要从文字的编码讲 ...

  3. python爬取某个网页的图片-如百度贴吧

    python爬取某个网页的图片-如百度贴吧 作者:vpoet mail:vpoet_sir@163.com 注:随意copy,不用告诉我 #coding:utf-8 import urllib imp ...

  4. Python:爬取乌云厂商列表,使用BeautifulSoup解析

    在SSS论坛看到有人写的Python爬取乌云厂商,想练一下手,就照着重新写了一遍 原帖:http://bbs.sssie.com/thread-965-1-1.html #coding:utf- im ...

  5. 使用python爬取MedSci上的期刊信息

    使用python爬取medsci上的期刊信息,通过设定条件,然后获取相应的期刊的的影响因子排名,期刊名称,英文全称和影响因子.主要过程如下: 首先,通过分析网站http://www.medsci.cn ...

  6. python爬取免费优质IP归属地查询接口

    python爬取免费优质IP归属地查询接口 具体不表,我今天要做的工作就是: 需要将数据库中大量ip查询出起归属地 刚开始感觉好简单啊,毕竟只需要从百度找个免费接口然后来个python脚本跑一晚上就o ...

  7. Python爬取豆瓣指定书籍的短评

    Python爬取豆瓣指定书籍的短评 #!/usr/bin/python # coding=utf-8 import re import sys import time import random im ...

  8. python爬取网页的通用代码框架

    python爬取网页的通用代码框架: def getHTMLText(url):#参数code缺省值为‘utf-8’(编码方式) try: r=requests.get(url,timeout=30) ...

  9. 没有内涵段子可以刷了,利用Python爬取段友之家贴吧图片和小视频(含源码)

    由于最新的视频整顿风波,内涵段子APP被迫关闭,广大段友无家可归,但是最近发现了一个"段友"的app,版本更新也挺快,正在号召广大段友回家,如下图,有兴趣的可以下载看看(ps:我不 ...

随机推荐

  1. CODECHEF Chef and Churus 解题报告

    [CODECHEF]Chef and Churus Description 有一个长度为\(n\)的数组\(A\),有\(n\)个函数,第\(i\)个函数的值为\(\sum_{j=l_i}^{r_i} ...

  2. Linux 内核分析第八周学习笔记

    Linux 内核分析第八周学习笔记 zl + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-10 ...

  3. vim在行首和 行尾加

    在每行开始加入“<a href=”   vim 命令:          :%s/^/<a href=/g 在每行尾加入 “</a>”    vim命令 :           ...

  4. 电子商务(电销)平台中用户模块(User)数据库设计明细

    以下是自己在电子商务系统设计中的订单模块的数据库设计经验总结,而今发表出来一起分享,如有不当,欢迎跟帖讨论~ 用户基础表(user_base)|-- 自动编号 (user_id)|-- 用户名 (us ...

  5. MapReduce(二)常用三大组件

    mapreduce三大组件:Combiner\Sort\Partitioner 默认组件:排序,分区(不设置,系统有默认值) 一.mapreduce中的Combiner 1.什么是combiner C ...

  6. Spark集群基础概念 与 spark架构原理

    一.Spark集群基础概念 将DAG划分为多个stage阶段,遵循以下原则: 1.将尽可能多的窄依赖关系的RDD划为同一个stage阶段. 2.当遇到shuffle操作,就意味着上一个stage阶段结 ...

  7. jq的图片放大镜效果

    <div class="imgbox"> <div class="probox"> <img src="" a ...

  8. 2017北京国庆刷题Day7 morning

    期望得分:100+0+100=200 实际得分:100+20+0=120 离散化搞搞 #include<cstdio> #include<iostream> #include& ...

  9. 重构改善既有代码设计--重构手法02:Inline Method (内联函数)& 03: Inline Temp(内联临时变量)

    Inline Method (内联函数) 一个函数调用的本体与名称同样清楚易懂.在函数调用点插入函数体,然后移除该函数. int GetRating() { return MoreThanfiveLa ...

  10. 【BZOJ】1951[Sdoi2010]古代猪文

    [题意]给定G,N,求: $$ans=G^{\sum_{i|n}\binom{n}{i}}\ \mod\ \ p$$ 1<=N,G<=10^9,p=999911659. [算法]欧拉定理+ ...