SBR是JOJO系列我最喜欢的一部,所以今天把漫画爬取到本地,日后慢慢看。

import re
import time
import requests
from requests import codes
from bs4 import BeautifulSoup
from requests import RequestException def get_page(url):
try:
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36'
+ '(KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.text
return None
except RequestException:
return None def get_pagesNumber(text):
soup = BeautifulSoup(text, 'lxml')
pagesNumber = soup.find(name='div', class_="d-none vg-r-data")
return pagesNumber.attrs['data-total'] def parse_page(text):
soup = BeautifulSoup(text, 'lxml')
url = soup.find(name='img', class_="img-fluid show-pic")
chapter = soup.find(name='h2', class_="h4 text-center")
page = soup.find(name='span', class_="c_nav_page")
yield {
'url': url['src'],
'chapter': chapter.get_text(),
'page': page.get_text()
}
#return 在返回结果后 结束函数的运行
#而yield 则是让函数变成一个生成器,生成器每次产生一个值,函数被冻结,被唤醒后再产生一个值 def save_image(item):
img_path = 'SBR' + os.path.sep + item.get('chapter') #os.path.sep是路径分隔符\
if not os.path.exists(img_path):
os.makedirs(img_path)
try:
resp = requests.get(item.get('url'))
if codes.ok == resp.status_code:
file_path = img_path + os.path.sep + '{file_name}.{file_suffix}'.format(
file_name=item.get('page'), file_suffix='jpg')
if not os.path.exists(file_path):
with open(file_path, 'wb') as f:
f.write(resp.content)
print('Downloaded image path is %s' % file_path)
else:
print('Already Downloaded', file_path)
except Exception as e:
print(e) if __name__ == '__main__':
for chapter in range(292, 316): #观察可发现共24章节,292到315 彩漫13283, 13306
url = 'https://www.manhuadb.com/manhua/147/4_'+str(chapter)+'.html'
text = get_page(url)
pagesNumber = get_pagesNumber(text) #获取当前章节总页数
for page in range(1,int(pagesNumber)+1):
url = 'https://www.manhuadb.com/manhua/147/4_'+str(chapter)+'_'+str(page)+'.html'
       #彩漫#url = 'https://www.manhuadb.com/manhua/147/1330_'+str(chapter)+'_'+str(page)+'.html'
text = get_page(url)
for item in parse_page(text):
save_image(item)

最后得到,

最新文章

  1. git的使用以及github
  2. Android开发自学笔记(Android Studio)—4.界面编程与View组件简单介绍
  3. Linux解压,压缩小总结
  4. winrt组件库(包括翻书组件)
  5. PAT (Basic Level) Practise:1038. 统计同成绩学生
  6. [转]解决WebClient或HttpWebRequest首次连接缓慢问题
  7. DHCP协议讲解
  8. Contest2037 - CSU Monthly 2013 Oct(中南大学2013年10月月赛水题部分题解)
  9. SharePoint 2010 master page 控件介绍(4):隐藏placeholder
  10. float与double的范围和精度(摘录)
  11. jquery +/-小样式
  12. Educational Codeforces Round 26-D. Round Subset
  13. TestNG 搭建测试框架 自动化测试
  14. 统计请求最高的TOP 5
  15. Android学习笔记PreferenceFragment的使用
  16. JSP内置对象之WEB安全性及config对象
  17. 适配器模式(PHP实现)
  18. Git学习系列之Git基本操作克隆项目(图文详解)
  19. Linux下的格式化字符串漏洞利用姿势
  20. org.xml.sax.SAXParseException; lineNumber: 14; columnNumber: 32; 元素类型为 "key" 的内容必须匹配 "(column)*"

热门文章

  1. Shell脚本 统计店中店导出数据
  2. android button的selector
  3. vuex 状态管理 入门
  4. 刷题85. Maximal Rectangle
  5. .net core 部署到IIS 后出现 w3wp.exe 【】发生了未经处理的win32异常……
  6. 流处理引擎(SPE)中的的分布式一致性语义之Exactly-Once和Effectively-Onece区别
  7. CHECK INDEX OF TABLE
  8. centos tomcat解压版安装
  9. C语言 getchar
  10. Enityt模型特性