实现效果

运行环境

  • IDE VS2019
  • Python3.7
  • Chrome、ChromeDriver
  • Chrome和ChromeDriver的版本需要相互对应

先上代码,代码非常简短,包含空行也才50行,多亏了python强大的库

import os
import time
import requests
from selenium import webdriver
from lxml import etree

def getChapterUrl(url):
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36"
}
part_url = "http://ac.qq.com"
res = requests.get(url, headers=headers)
html=res.content.decode()
el = etree.HTML(html)
li_list = el.xpath('//*[@id="chapter"]/div[2]/ol[1]/li')
for li in li_list:
for p in li.xpath("./p"):
for span in p.xpath("./span[@class='works-chapter-item']"):
item = {}
list_title = span.xpath("./a/@title")[0].replace(' ', '').split(':')
if list_title[1].startswith(('第', '序')):
getChapterFile(part_url + span.xpath("./a/@href")[0], list_title[0],list_title[1])

def getChapterFile(url,path1,path2):
#path = os.path.join(path)
#漫画名称目录
path=os.path.join(path1)
if not os.path.exists(path):
os.mkdir(path)
#章节目录
path=path+'\\'+path2
if not os.path.exists(path):
os.mkdir(path)
chrome=webdriver.Chrome()
#"http://ac.qq.com/ComicView/index/id/505435/cid/2"
chrome.get(url)
time.sleep(4)
imgs = chrome.find_elements_by_xpath("//div[@id='mainView']/ul[@id='comicContain']//img")
for i in range(0, len(imgs)):
js="document.getElementById('mainView').scrollTop="+str((i) * 1280)
chrome.execute_script(js)
time.sleep(3)
print(imgs[i].get_attribute("src"))
with open(path+'\\'+str(i)+'.png', 'wb') as f:
f.write(requests.get(imgs[i].get_attribute("src")).content)
chrome.close()
print('下载完成')

if __name__ == '__main__':
getChapterUrl('http://ac.qq.com/Comic/ComicInfo/id/505435')

简单解释

输入一个漫画的url即可爬取该漫画所有的章节,由于是模拟用户爬取的,所以速度方面有点慢,我试了下爬取银魂前70章,用了1个半小时,代码中的sleep可以适当简短点已加快爬取的速度

付费的漫画是没有办法爬取的

最新文章

  1. 由Selenium1转变为Selenium2所遇到的问题
  2. springboot maven install 找不到符号
  3. 快速幂 fast_exp
  4. EditText小技巧
  5. 总线(BUS)和总线操作
  6. 教你50招提升ASP.NET性能(十一):避免在调试模式下运行网站
  7. java_Cookies_1_商品浏览历史记录servlet2
  8. 3. NHibernate基础知识 - 你必须知道的一些事情
  9. 关于IE7 兼容问题
  10. Android调用系统的Activity、ContentProvider、Service、Broadcast Receiver
  11. block 的调用 简单代码
  12. .net技术深入
  13. 我的第一本docker书-阅读笔记
  14. 修复python的ModuleNotFoundError
  15. grpc 入门(一)--hello world
  16. 【莫比乌斯反演】BZOJ3309 DZY Loves Math
  17. Eclipse安装hibernate插件
  18. 我的Python笔记补充:入门知识拾遗
  19. 2018.11.06 bzoj1835: [ZJOI2010]base 基站选址(线段树优化dp)
  20. 项目中经常用到的JavaScript方法

热门文章

  1. ASP.NET MVC升级到ASP.NET Core MVC踩坑小结
  2. 文献名:Repeat-Preserving Decoy Database for False Discovery Rate Estimation in Peptide Identication (用于肽段鉴定中错误发生率估计的能体现重复性的诱饵数据库)
  3. doc-指令-查看端口是否被占用及占用程序
  4. Azure Web: 数据库的创建与数据监控
  5. PHP7内核(五):系统分析生命周期
  6. vs编译器中不允许使用scanf()的情况
  7. C - Monkey and Banana
  8. win10配置易用命令行
  9. 痞子衡嵌入式:简析i.MXRT1170 Cortex-M7 FlexRAM ECC功能特点、开启步骤、性能影响
  10. 【STM32项目笔记】STM32CubeMX+Keil+Proteus联合实现LED闪烁