中国漫画一直以来都被日本漫画洗涤着,而近几年日本漫画逐渐退出中国市场,中国优秀的原创漫画作者和原创漫画作品层出不穷。
随着国内动漫行业的迅速发展,我国的二次元消费群体正在不断扩张,国产漫画的发展也进入新的时期。随着近两年爆款作品的陆续出现,国漫已经逐步摆脱了“稚嫩”和“制作粗。
不难看出,原创漫画业,这个在中国沉寂了许久的新型产业,已经蓄势而发。喜欢和从事原创漫画创作的人也逐渐在增多。
接下来让我们步入今天的正题,使用python爬取漫画,此次代码以一个小型的网站来作为练手,其他的和该思路差不多大致相同,仅供参考。
https://m.gufengmh8.com/
打开这个网址后,让我们点击搜索进行相关的漫画搜索。截图为搜索页面,可以看到网址为:
https://m.gufengmh8.com/search/?keywords=斗罗大陆
keywords后面跟的就是要搜索的内容,然后我们获取url的方式就可以是这样。
"https://m.gufengmh8.com/manhua/search/?keywords="+str(input("搜索漫画:"))#input让用户输入,获取输入内容
然后我们开始对这个页面进行剖析,我们要获取的内容有哪些呢,在这里就不写太复杂,只爬取漫画名供用户选择就行,毕竟同名的漫画也不多,浏览器按f12进入代码调试,单击下图位置,然后可以看到class为itemBox,所以我们只需要获取到这个页面所有的class为itemBox的div,就可以获取每本漫画的所有信息。
在这里只取漫画名,再用小箭头点击漫画名,可以看到a标签下的就是要获取的漫画名,所以逻辑就清晰了,先获取class,然后遍历class获取到每个class中的itemTxt,然后再获取到itemTxt的第一个节点。
import math
import threading
import time
import os
import requests
from bs4 import BeautifulSoup
from urllib3.connectionpool import xrange
#根据url获取对应页面的所有内容,然后返回
def get_document(url):
# print(url)
try:
get = requests.get(url)#打开连接
data = get.content#获取内容
get.close()#关闭连接
except:#抛异常就重试
time.sleep(3)#睡眠3秒,给网页反应时间
try:再次获取
get = requests.get(url)
data = get.content
get.close()
except:
time.sleep(3)
get = requests.get(url)
data = get.content
get.close()
return data
#下载漫画
def download_img(html):
soup = BeautifulSoup(html)#BeautifulSoup和request搭配使用更佳呦
itemBox = soup.find_all('div', attrs={'class': 'itemBox'})#find_all返回的是一个list
for index, item in enumerate(itemBox):#遍历itemBox,index是当前项list的下标,item是内容
itemTxt = item.find('div', attrs={'class': 'itemTxt'})#因为只有一个,所以itemBox中只有一个itemTxt所以这次我们用find
a = itemTxt.find('a', attrs={'class': 'title'}).text[]
print(str(index+1)+'.'+a)
# download_img(get_document("https://m.gufengmh8.com/search/?keywords="+str(input("搜索漫画:"))))
download_img(get_document("https://m.gufengmh8.com/search/?keywords=完美世界"))#这个就不解释了吧
现在我们基本实现了搜索功能,这已经算是个简单爬虫了,之后让用户输入书籍序号,然后下载。
我们随便点进去一本漫画,用之前的方式获取到id为chapter-list-1的ul包含了所有的章节,ul中的每一个li又包含一个a标签和span标签,分别是url和章节名,之后就可以继续写了。
def download_img(html):
chapter_url_list=[]
soup = BeautifulSoup(html)#BeautifulSoup和request搭配使用更佳呦
itemBox = soup.find_all('div', attrs={'class': 'itemBox'})#find_all返回的是一个list
for index, item in enumerate(itemBox):#遍历itemBox,index是当前项list的下标,item是内容
itemTxt = item.find('div', attrs={'class': 'itemTxt'})#因为只有一个,所以itemBox中只有一个itemTxt所以这次我们用find
a = itemTxt.find('a', attrs={'class': 'title'})
chapter_url = a['href']
chapter_url_list.append(chapter_url)#把所有书的url存起来
print(str(index+1)+'.'+a.text)
number = int(input('请输入漫画序号'))
chapter_html = BeautifulSoup(get_document(chapter_url_list[number-1]))#因为打印的序号和list的索引是相差1的,所以输入的序号减一获取对应书的url,再根据url获取到目录页面
ul = chapter_html.find('ul', attrs={'id': 'chapter-list-1'})#获取到ul
li_list = ul.find_all('li')#获取其中所有li
for li in li_list:#遍历
li_a_href = li.find('a')['href']#注意这里获取到的url是不完整的/manhua/buhuochongwuniangdezhengquefangfa/1000845.html
现在我们随便点入一个章节获取到漫画图片的位置。
然后我们终于获取到了图片的src,但是还有个问题,他是分页的,所以…
仔细钻研后发现如果当前页不存在时会显示这个图片,那我们就一直循环,直到获取的到的图片是这个时,结束循环,也就是这个样子。
while True:
li_a_href_replace = li_a_href
if i != 0:#不加-i就是第一页
li_a_href_replace = li_a_href.replace('.', ('-' + str(i) + '.'))#https://m.gufengmh8.com/manhua/wanmeishijiePERFECTWORLD/549627.html把"."换成"-1."https://m.gufengmh8.com/manhua/wanmeishijiePERFECTWORLD/549627-1.html就是第二页了
print(li_a_href_replace)
chapter_html = BeautifulSoup(get_document('https://m.gufengmh8.com' + li_a_href_replace))
chapter_content = chapter_html.find('div', attrs={'class': 'chapter-content'})
img_src = chapter_content.find('img')['src']
if img_src.__eq__('https://res.xiaoqinre.com/images/default/cover.png'):
break
接着我们获取到了所有的漫画图片src,现在就只需要把他下载下来了,先创建目录。
path = "d:/SanMu/"+book_name+'/'+li.text.replace('\n', '')
if not os.path.exists(path):
os.makedirs(path)
然后下载,很简单吧。
open(path+'/'+str(i)+'.jpg', 'wb').write(get_document(img_src))#保存到d:/SanMu/书名/章节名/0.jpg
最后放出综合代码:
import math
import threading
import sys, time
import os
import requests
from bs4 import BeautifulSoup
from urllib3.connectionpool import xrange
from tqdm import tqdm
class myThread (threading.Thread):
def __init__(self, split_dds, name, num):
threading.Thread.__init__(self)
self.name = name
self.split_dds = split_dds
self.num = num
def run(self):
print("开始线程:" + self.num)
save_img(self.split_dds, self.name)
print("退出线程:" + self.num)
def split_list(ls, each):
list = []
eachExact = float(each)
groupCount = int(len(ls) // each)
groupCountExact = math.ceil(len(ls) / eachExact)
start = 0
for i in xrange(each):
if i == each - 1 & groupCount < groupCountExact: # 假如有余数,将剩余的所有元素加入到最后一个分组
list.append(ls[start:len(ls)])
else:
list.append(ls[start:start + groupCount])
start = start + groupCount
return list
def get_document(url):
# print(url)
try:
get = requests.get(url)
data = get.content
get.close()
except:
time.sleep(3)
try:
get = requests.get(url)
data = get.content
get.close()
except:
time.sleep(3)
get = requests.get(url)
data = get.content
get.close()
return data
def save_img(li_list_split, book_name):
for num in range(len(li_list_split)):#遍历
li=li_list_split[num]
li_a_href = li.find('a')['href']#注意这里获取到的url是不完整的/manhua/buhuochongwuniangdezhengquefangfa/1000845.html
i = 0
path = "d:/SanMu/"+book_name+'/'+li.text.replace('\n', '')
if not os.path.exists(path):
os.makedirs(path)
while True:
li_a_href_replace = li_a_href
if i != 0:
li_a_href_replace = li_a_href.replace('.', ('-' + str(i) + '.'))
chapter_html = BeautifulSoup(get_document('https://m.gufengmh8.com' + li_a_href_replace), 'lxml')
chapter_content = chapter_html.find('div', attrs={'class': 'chapter-content'})
img_src = chapter_content.find('img')['src']
if img_src.__eq__('https://res.xiaoqinre.com/images/default/cover.png'):
break
chapter_content = chapter_html.find('div', attrs={'class': 'chapter-content'})
img_src = chapter_content.find('img')['src']
open(path+'/'+str(i)+'.jpg', 'wb').write(get_document(img_src))#保存到d:/SanMu/书名/章节名/0.jpg
i += 1
def download_img(html):
chapter_url_list=[]
soup = BeautifulSoup(html, 'lxml')#BeautifulSoup和request搭配使用更佳呦
itemBox = soup.find_all('div', attrs={'class': 'itemBox'})#find_all返回的是一个list
for index, item in enumerate(itemBox):#遍历itemBox,index是当前项list的下标,item是内容
itemTxt = item.find('div', attrs={'class': 'itemTxt'})#因为只有一个,所以itemBox中只有一个itemTxt所以这次我们用find
a = itemTxt.find('a', attrs={'class': 'title'})
chapter_url = a['href']
chapter_url_list.append(chapter_url)#把所有书的url存起来
print(str(index+1)+'.'+a.text)
number = int(input('请输入漫画序号'))
chapter_html_list = BeautifulSoup(get_document(chapter_url_list[number-1]), 'lxml')#因为打印的序号和list的索引是相差1的,所以输入的序号减一获取对应书的url,再根据url获取到目录页面
ul = chapter_html_list.find('ul', attrs={'id': 'chapter-list-1'})#获取到ul
book_name = chapter_html_list.find('h1', attrs={'class': 'title'}).text#获取到ul
li_list = ul.find_all('li')#获取其中所有li
thread_list = []
thread_count = split_list(li_list, len(li_list))#多少章就启多少个线程len(li_list)可以改成固定的线程数,
for num, li_list_split in enumerate(thread_count):#拆分了多少个list就创建多少个线程
thread = myThread(li_list_split, book_name, str(num))
thread_list.append(thread)
for thread in thread_list:
thread.start()
for thread in thread_list:
thread.join()
while 1:
break
download_img(get_document("https://m.gufengmh8.com/search/?keywords="+str(input("搜索漫画:"))))
右击运行代码,根据你的需求下载你想要下载的漫画,以上就是今天给大家分享的内容,源代码获取请回复 “ 漫画下载 ” 。
当下,希望中国的原创漫画之路能日渐强大。也能出现象火影忍者漫画,海贼王漫画,七龙珠漫画,灌篮高手漫画,蜡笔小新漫画等优秀热门漫画。这些曾影响了一代人,而今后的中国原创漫画,将会影响着更多的人。