一.抓包獲取連結

以爬取《前科者》為例

獲取搜尋連結

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

侵權刪!請注意版權!僅供自己的程式設計學習與測試,不要將漫畫進行傳播,更不要牟利!尊重原作和內容提供商!