一.抓包获取链接

以爬取《前科者》为例

获取搜索链接

https://api.copymanga.com/api/v3/search/comic?limit=5&q=前科者

获取漫画详细页面

https://api.copymanga.com/api/v3/comic/qiankezhe/group/default/chapters?limit=200

第一话的链接

https://api.copymanga.com/api/v3/comic/qiankezhe/chapter2/52f9d522-0d71-11eb-93ea-00163e0ca5bd

抓包过程中因为headers不同响应也不同,所以需要插入一句

headers = {"Uesr-Agent" : "Dart/2.10 (dart:io)", "region" : "1"}

那么先引用requests模块,写出请求函数和创建目录函数

#请求
def request_get(url):
try:
headers = {"Uesr-Agent" : "Dart/2.10 (dart:io)", "region" : "1"}
request_str = requests.get(url,headers=headers)
request_str.encoding = 'utf-8-sig'
return request_str
except:
print("访问失败,请检查网络")
sys.exit()
#创建目录
def mkdir(path):
folder = os.path.exists(path)
if not folder:
os.makedirs(path)
else:
    print(path + "目录已存在")

二.处理搜索链接及其参数

利用抓包软件,获得搜索链接

https://api.copymanga.com/api/v3/search/comic?limit=5&q=前科者

其中参数limit为显示漫画搜索结果的数目,这个是模糊搜索,参数q就是搜索内容

既然是模糊搜索,那么当然要手动去app搜索一下,并几下想要的漫画排在第几

我们先把limit参数改为1,让链接只返回一部漫画,方便观察响应:

 1 {
2 "code": 200,
3 "message": "请求成功",
4 "results": {
5 "list": [
6 {
7 "cover": "https://mirror2.mangafunc.fun/comic/qiankezhe/cover/3c1a6b4c-0d6b-11eb-b49c-00163e0ca5bd.jpg!kb_m_item",
8 "img_type": 2,
9 "author": [
10 {
11 "name": "月岛冬二",
12 "alias": null,
13 "path_word": "yuedaodonger"
14 },
15 {
16 "name": "香川まさひと",
17 "alias": null,
18 "path_word": "xiangchuanirihm"
19 }
20 ],
21 "name": "前科者",
22 "alias": "前科者,前科者",
23 "path_word": "qiankezhe",
24 "popular": 13042
25 }
26 ],
27 "total": 1816,
28 "limit": 1,
29 "offset": 0
30 }
31 }

观察漫画详细页面的链接里面的qiankezhe与path_word对应,漫画名字对应name

那么我们根据这个模糊搜索,直接利用json模块获取这两个参数,并存放在name_words和names字典中,并对应上搜索结果的排列顺序,代码:

 1 try:
2 name = input("请输入漫画名字:")
3 th = int(input("请输入漫画搜索结果排序位置(数字):"))
4 except:
5 print("输入错误")
6 sys.exit()
7
8 search_url = "https://api.copymanga.com/api/v3/search/comic?limit=10&q={}".format(name)
9 search_str = request_get(search_url)
10 name_str = json.loads(search_str.text).get("results").get("list")
11 name_words = {}
12 names = {}
13 num = 1
14 for i in name_str:
15 name_words.update({num : i.get("path_word")})
16 names.update({num : i.get("name")})
17 num += 1

三.处理漫画详细页面及其参数

https://api.copymanga.com/api/v3/comic/qiankezhe/group/default/chapters?limit=200

观察一下链接,limit明显是显示多少话,中间的qiankezhe在上面的name_words字典中

那么修改limit为1,来看一下响应(记得用json格式化,让响应好看一点):

 1 {
2 "code": 200,
3 "message": "\u8bf7\u6c42\u6210\u529f",
4 "results": {
5 "list": [{
6 "index": 0,
7 "uuid": "52f9d522-0d71-11eb-93ea-00163e0ca5bd",
8 "count": 28,
9 "ordered": 10,
10 "size": 41,
11 "name": "第一话",
12 "comic_id": "3c192200-0d6b-11eb-b49c-00163e0ca5bd",
13 "comic_path_word": "qiankezhe",
14 "group_id": null,
15 "group_path_word": "default",
16 "type": 1,
17 "img_type": 2,
18 "datetime_created": "2020-10-14",
19 "prev": null,
20 "next": "cffb84a2-15d8-11eb-9e11-00163e0ca5bd"
21 }],
22 "total": 28,
23 "limit": 1,
24 "offset": 0
25 }
26 }

看到里面的uuid与第一话链接对应52f9d522-0d71-11eb-93ea-00163e0ca5bd

https://api.copymanga.com/api/v3/comic/qiankezhe/chapter2/52f9d522-0d71-11eb-93ea-00163e0ca5bd

那么我们创建这部漫话的目录,然后获取uuid和name(这个name就是第几话),再创建'第几话'目录,方便存放图片:

#创建漫画目录
name_path = path + r'\{}'.format(names[th])
mkdir(name_path)
#获取每一话的uuid
uuid_url = "https://api.copymanga.com/api/v3/comic/{}/group/default/chapters?limit=200".format(name_words[th])
uuid_str = request_get(uuid_url)
uuid_str = json.loads(uuid_str.text).get("results").get("list")
for i in uuid_str:
chaper = i.get("name")
uuid = i.get("uuid")
chaper_path = name_path + r"\{}".format(chaper)
mkdir(chaper_path) #创建'第几话'目录
down(uuid) #加入用down函数下载图片(down函数暂未写出)

四.编写下载图片的down函数

先看一下第一话的链接

https://api.copymanga.com/api/v3/comic/qiankezhe/chapter2/52f9d522-0d71-11eb-93ea-00163e0ca5bd

qiankezhe52f9d522-0d71-11eb-93ea-00163e0ca5bd已经在上面获取了,分别为name_words字典和字符串uuid

再看看响应(这个contents太长了,我就删除一些了):

 1 {
2 "code": 200,
3 "message": "\u8bf7\u6c42\u6210\u529f",
4 "results": {
5 "show_app": false,
6 "is_lock": false,
7 "is_login": false,
8 "is_mobile_bind": false,
9 "is_vip": false,
10 "comic": {
11 "name": "\u524d\u79d1\u8005",
12 "uuid": "3c192200-0d6b-11eb-b49c-00163e0ca5bd",
13 "path_word": "qiankezhe",
14 "restrict": {
15 "value": 0,
16 "display": "\u4e00\u822c\u5411(\u514d\u8cbb)"
17 }
18 },
19 "chapter": {
20 "index": 0,
21 "uuid": "52f9d522-0d71-11eb-93ea-00163e0ca5bd",
22 "count": 28,
23 "ordered": 10,
24 "size": 41,
25 "name": "\u7b2c01\u8bdd",
26 "comic_id": "3c192200-0d6b-11eb-b49c-00163e0ca5bd",
27 "comic_path_word": "qiankezhe",
28 "group_id": null,
29 "group_path_word": "default",
30 "type": 1,
31 "img_type": 2,
32 "datetime_created": "2020-10-14",
33 "prev": null,
34 "next": "cffb84a2-15d8-11eb-9e11-00163e0ca5bd",
35 "contents": [{
36 "uuid": "8aff0712-0d71-11eb-9be4-00163e0ca5bd",
37 "url": "https://mirror2.mangafunc.fun/comic/qiankezhe/420b9/8afbbff8-0d71-11eb-9be4-00163e0ca5bd.jpg!kb_m_read_large"
38 }],
39 "words": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 30, 38, 39, 21, 31, 27, 32, 34, 23, 26, 28, 24, 40, 36, 20, 25, 33, 29, 22, 35, 37],
40 "is_long": false
41 }
42 }
43 }

可以看到contents里面的url就是每一张图片的链接,那么直接获取这个链接并下载到本地就行了

看代码:

 1 def down(uuid):
2 get_pictrue_url = "https://api.copymanga.com/api/v3/comic/{}/chapter2/{}".format(name_words,uuid)
3 picture_str = request_get(get_pictrue_url)
4 picture_str = json.loads(picture_str.text).get("results").get("chapter").get("contents")
5 num = 1
6 for i in picture_str:
7 picture = request_get(i.get("url"))
8 with open(chaper_path + "\{}.jpg".format(num),"wb") as code:
9 code.write(picture.content)
10 num = num + 1
 

结尾:

这样就可以了,我们引用了4个库:requests, sys, os, json

整合全部代码:

 1 #!/usr/bin/python
2 # -*- coding: UTF-8 -*-
3 import requests
4 import sys
5 import os
6 import json
7
8 #############################配置区##############################
9 #保存目录
10 path = r"D:\0file\Download"
11 #################################################################
12
13 #################################################################
14 ############################代码区###############################
15 try:
16 name = input("请输入漫画名字:")
17 th = int(input("请输入漫画搜索结果排序位置(数字):"))
18 except:
19 print("输入错误")
20 sys.exit()
21
22 #—————————————————————————————函数区————————————————————————————#
23 #创建目录
24 def mkdir(path):
25 folder = os.path.exists(path)
26 if not folder:
27 os.makedirs(path)
28 else:
29 print (path + "目录已存在")
30 #请求
31 def request_get(url):
32 try:
33 headers = {"Uesr-Agent" : "Dart/2.10 (dart:io)", "region" : "1"}
34 request_str = requests.get(url,headers=headers)
35 request_str.encoding = 'utf-8-sig'
36 return request_str
37 except:
38 print("访问失败,请检查网络")
39 sys.exit()
40 #下再图片
41 def down(uuid):
42 get_pictrue_url = "https://api.copymanga.com/api/v3/comic/{}/chapter2/{}".format(name_words,uuid)
43 picture_str = request_get(get_pictrue_url)
44 picture_str = json.loads(picture_str.text).get("results").get("chapter").get("contents")
45 num = 1
46 for i in picture_str:
47 picture = request_get(i.get("url"))
48 with open(chaper_path + "\{}.jpg".format(num),"wb") as code:
49 code.write(picture.content)
50 num = num + 1
51 #——————————————————————————————————————————————————————————————#
52
53 #获取搜索结果
54 search_url = "https://api.copymanga.com/api/v3/search/comic?limit=10&q={}".format(name)
55 search_str = request_get(search_url)
56 name_str = json.loads(search_str.text).get("results").get("list")
57 name_words = {}
58 names = {}
59 num = 1
60 for i in name_str:
61 name_words.update({num : i.get("path_word")})
62 names.update({num : i.get("name")})
63 num += 1
64
65 #创建漫画目录
66 name_path = path + r'\{}'.format(names[th])
67 mkdir(name_path)
68 #获取每一话的uuid
69 uuid_url = "https://api.copymanga.com/api/v3/comic/{}/group/default/chapters?limit=200".format(name_words[th])
70 uuid_str = request_get(uuid_url)
71 uuid_str = json.loads(uuid_str.text).get("results").get("list")
72 for i in uuid_str:
73 chaper = i.get("name")
74 uuid = i.get("uuid")
75 chaper_path = name_path + r"\{}".format(chaper)
76 mkdir(chaper_path)
77 down(uuid)
78 #################################################################
79 #################################################################
80

侵权删!请注意版权!仅供自己的编程学习与测试,不要将漫画进行传播,更不要牟利!尊重原作和内容提供商!

实战爬取拷背漫画-Python的更多相关文章

  1. 实战爬取某网站图片-Python

    直接上代码 1 #!/usr/bin/python 2 # -*- coding: UTF-8 -*- 3 from bs4 import BeautifulSoup 4 import request ...

  2. python爬虫实战---爬取大众点评评论

    python爬虫实战—爬取大众点评评论(加密字体) 1.首先打开一个店铺找到评论 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经 ...

  3. 第三百三十节,web爬虫讲解2—urllib库爬虫—实战爬取搜狗微信公众号—抓包软件安装Fiddler4讲解

    第三百三十节,web爬虫讲解2—urllib库爬虫—实战爬取搜狗微信公众号—抓包软件安装Fiddler4讲解 封装模块 #!/usr/bin/env python # -*- coding: utf- ...

  4. 九 web爬虫讲解2—urllib库爬虫—实战爬取搜狗微信公众号—抓包软件安装Fiddler4讲解

    封装模块 #!/usr/bin/env python # -*- coding: utf-8 -*- import urllib from urllib import request import j ...

  5. 简单的scrapy实战:爬取腾讯招聘北京地区的相关招聘信息

    简单的scrapy实战:爬取腾讯招聘北京地区的相关招聘信息 简单的scrapy实战:爬取腾讯招聘北京地区的相关招聘信息 系统环境:Fedora22(昨天已安装scrapy环境) 爬取的开始URL:ht ...

  6. Python爬虫,爬取腾讯漫画实战

    先上个爬取的结果图 最后的结果为每部漫画按章节保存 运行环境 IDE VS2019 Python3.7 先上代码,代码非常简短,包含空行也才50行,多亏了python强大的库 import os im ...

  7. 教程+资源,python scrapy实战爬取知乎最性感妹子的爆照合集(12G)!

    一.出发点: 之前在知乎看到一位大牛(二胖)写的一篇文章:python爬取知乎最受欢迎的妹子(大概题目是这个,具体记不清了),但是这位二胖哥没有给出源码,而我也没用过python,正好顺便学一学,所以 ...

  8. python爬虫入门新手向实战 - 爬取猫眼电影Top100排行榜

    本次主要爬取Top100电影榜单的电影名.主演和上映时间, 同时保存为excel表个形式, 其他相似榜单也都可以依葫芦画瓢 首先打开要爬取的网址https://maoyan.com/board/4, ...

  9. python实战===爬取所有微信好友的信息

    ''' 爬取所有T信好友的信息 ''' import itchat from pandas import DataFrame itchat.login() friends=itchat.get_fri ...

随机推荐

  1. Python Tkinter Menu

    本人想开发一个简易的搜图GUI,基于此,选择用Tkinter模块开发. 需要开发出菜单栏 1 from Tkinter import * 2 3 4 root = Tk() 5 root.title( ...

  2. Auto update Python 2.x to 3.x

    1, How to check the python version import sys if sys.version_info < (3.0)     print ("python ...

  3. php 扩展kafka

    一.安装librdkafka cd /usr/local/src/ git clone https://github.com/edenhill/librdkafka.git cd librdkafka ...

  4. php图片木马实现原理

    什么是木马 木马病毒是指隐藏在正常程序中的一段具有特殊功能的恶意代码,是具备破坏和删除文件.发送密码.记录键盘和攻击Dos等特殊功能的后门程序. 那,php的木马是长什么样的呢?我们来看下面这段代码: ...

  5. Could not connect to 'xxx.xx.xx.xxx' (port 22): Connection failed.

    刚刚使用xshell好好的,突然注销账号,准备重新连接突然连不上了. 这就很尴尬了,对我这种linux菜鸟只能去百度了,终于解决了,赶紧记录下这个坑 1.先登陆虚拟机,输入这段命令 查看ssh服务是否 ...

  6. buu 红帽杯 XX

    一.拖入ida,静态分析 __int64 __fastcall sub_7FF65D4511A0(__int64 a1, __int64 a2) { signed __int64 v2; // rbx ...

  7. JetBrains GoLand 以debug运行Go程序时出现could not launch process: decoding dwarf section info at offset 0x0: too short报错之保姆级别解决方案

    这是一篇写给刚开始学习Go语言而在搭建环境可能遇到问题的小萌新的文,大神请自行绕路哈(0-0) 有天,我把Go运用环境升到最新版1.16后,用以前一直在用的JetBrains GoLand 2017. ...

  8. 攻防世界-crypto-Decrypt-the-Message(Poem Codes-诗歌密码)

    题目来源:su-ctf-quals-2014题目描述:解密这段信息! 下载附件,内容如下 The life that I have Is all that I have And the life th ...

  9. PYTHON 得到光标处的句柄

    import win32api import win32gui import time if __name__ == '__main__': while True: point = win32api. ...

  10. Java核心基础第5篇-Java面向对象_类和对象

    Java面向对象之类和对象 一.面向对象概述 Java是完全的面向对象编程(Object Oriented Programming),简称OOP. 面向对象编程的思维方式更加符合大家的日常生活,因为我 ...