赞
踩
漫画是什么?
丰子恺说:
“漫画是简笔而注重意义的一种绘画。
如今漫画深受大众喜欢,今天
我们就来采集一下漫画数据吧,免费看完本
Python 3.8及Pycharm
requests >>>
pip install requests 数据请求模块
parsel >>>
pip install parsel 数据解析模块
二. 代码实现步骤
获取章节ID/章节名字/漫画名字:
请求链接: 漫画目录页url
获取数据, 获取服务器返回响应数据
3.解析数据, 提取我们想要的数据内容
4.保存数据
- # 导入数据请求模块 --> 第三方模块, 需要安装 pip install requests
- import requests
- # 导入数据解析模块 --> 第三方模块, 需要安装 pip install parsel
- import parsel
- # 导入文件操作模块 --> 内置模块, 不需要安装
- import os
-
- # 请求链接 <目录页url>
- url = 'https://www.******.com/208707/'
- # 伪装模拟
- headers = {
- # user-agent 用户代理, 表示浏览器基本身份信息 Chrome 浏览器名字 101.0.0.0 浏览器版本 Windows 电脑系统
- 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36'
- }
- # 发送请求
- response = requests.get(url=url, headers=headers)
- 源码、解答、教程、资料加Q君羊:582950881##
- # 把获取下来html字符串数据内容<response.text>, 转换成可解析对象
- selector = parsel.Selector(response.text) # <Selector xpath=None data='<html>\n <head>\n <title>网游之近战法师_网游...'>
- # 提取漫画名字
- name = selector.css('.de-info__box .comic-title::text').get()
-
- # 自动创建文件夹 以漫画名字作为文件夹名
- file = f'{name}\\'
- if not os.path.exists(file):
- os.mkdir(file)
-
-
- # 第一次提取所有li标签, 返回是对象, 我不需要提取li标签里的内容
- lis = selector.css('.chapter__list .chapter__list-box .chapter__item')
- # 如何一个一个提取列表当中元素? for循环遍历
- for li in list(reversed(lis)):
- # 提取章节ID +为什么加的意思
- chapter_id = li.css('a::attr(data-chapterid)').get()
- # 提取章节名字
- chapter_title = li.css('a::text').getall()[-1].strip()
- # 请求链接: 漫画集合链接 --> f'{}' 字符串格式化方法, 相当于, 把chapter_id传入link这段字符串当中
- link = f'https://comic.******.com/chapter/content/v1/?chapter_id={chapter_id}&comic_id=208707&format=1&quality=1&sign=5a5b72c44ad43f6611f1e46dd4d457bf&type=1&uid=61003965'
- # 发送请求
- 源码、解答、教程、资料加Q君羊:582950881##
- json_data = requests.get(url=link, headers=headers).json()
- num = 1
- print(chapter_title)
- # for循环遍历, 一个一个提取列表元素
- for index in json_data['data']['page']:
- # 字典取值
- image = index['image']
- img_content = requests.get(url=image).content
- with open(file + chapter_title + str(num) + '.jpg', mode='wb') as f:
- f.write(img_content)
- num += 1
- print(image)
好啦,本文章到这里就结束拉
有喜欢的小伙伴记得给博主一个三连哦~
希望你在学习的路上不忘初心,坚持不懈,学有所成
把时间和精力,放在自己擅长的方向,去坚持与努力,
如果不知道自己擅长什么,就尽快找到它。
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。