.前言

说实话这次的爬虫可能是目前我遇到的最难的一个爬虫,主要之前爬取的都是一些静态资源的网站,这次的网站虽然 反爬机制 虽然也只是低层次的,但是对于新手的我来说也算是比较难的了。

2.反爬过程

这样我们便能通过xpath直接定位得到了

def getLinks(html):

chapter_link=[]

chapter_title=[]

parse=parsel.Selector(html)

links=parse.xpath('//div[@class="tab-content tab-content-selected zj_list_con autoHeight"]/ul[@class="list_con_li autoHeight"]/li/a/@href').getall()

titles=parse.xpath('//div[@class="tab-content tab-content-selected zj_list_con autoHeight"]/ul[@class="list_con_li autoHeight"]/li/a/span[@class="list_con_zj"]/text()').getall()

for link in links:

chapter_link.insert(0,link)

for title in titles:

chapter_title.insert(0, title)

return chapter_link,chapter_title

1234567891011

只要注意他这里章节是降序排列的,所以我们爬取的过程中需要将他翻转过来,所以不能只用 append 方法,应用 insert 方法。

源码

我的代码:

import requests

import parsel

import pypinyin

from bs4 import BeautifulSoup

import re

import os

import time

# 伪装浏览器。设置请求头

headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36",}

# 返回网页的请求信息

def askUrl(url):

response=requests.get(url,headers=headers)

html=response.content.decode('utf-8')

return html

# 获取所有的章节链接以及章节名称

def getLinks(html):

chapter_link=[]

chapter_title=[]

parse=parsel.Selector(html)

links=parse.xpath('//div[@class="tab-content tab-content-selected zj_list_con autoHeight"]/ul[@class="list_con_li autoHeight"]/li/a/@href').getall()

titles=parse.xpath('//div[@class="tab-content tab-content-selected zj_list_con autoHeight"]/ul[@class="list_con_li autoHeight"]/li/a/span[@class="list_con_zj"]/text()').getall()

for link in links:

chapter_link.insert(0,link)

for title in titles:

chapter_title.insert(0, title)

return chapter_link,chapter_title

# 获取所有漫画的链接

def getImgs(link):

pic_url=[]

response=requests.get(link,headers=headers)

html=BeautifulSoup(response.text,'lxml')

script_info=html.script

one = re.findall("\|(\d{4})\|", str(script_info))[0]

two = re.findall("\|(\d{5})\|", str(script_info))[0]

threes=re.findall('\d{13,14}',str(script_info))

for i, three in enumerate(threes):

if len(three) == 13:

threes[i] = three + '0'

threes = sorted(threes, key=lambda x: int(x))

for three in threes:

if three[-1]=='0':

pic_url.append("https://images.dmzj.com/img/chapterpic/"+one+"/"+two+"/"+three[:-1]+".jpg")

else:

pic_url.append("https://images.dmzj.com/img/chapterpic/" + one + "/" + two + "/" + three + ".jpg")

return pic_url

# 下载漫画

def download(url,links,dir_name):

headers1={

'Referer': url,

}

i=1;

for link in links:

pic_name = '%03d.jpg' % (i)

new_dir_name = os.path.join(dir_name, pic_name)

response=requests.get(link,headers=headers1)

with open(new_dir_name, 'wb')as f:

f.write(response.content)

print(pic_name+"下载完成")

i+=1

# main方法

def main():

manhuas=input("请输入你要下载的漫画名:")

dir_name = r'D:\漫画'

if not os.path.exists(dir_name + './' + manhuas):

os.makedirs(dir_name + './' + manhuas)

dir_name=dir_name + './' + manhuas

manhuas=pypinyin.pinyin(manhuas,style=pypinyin.NORMAL)

name=''

for manhua in manhuas:

name=name+''.join(manhua)

url="https://www.dmzj.com/info/"+name+".html"

html=askUrl(url)

links=getLinks(html)[0]

names = getLinks(html)[1]

for i,link in enumerate(links):

if not os.path.exists(dir_name + './' + str(names[i])):

os.makedirs(dir_name + './' + str(names[i]))

print("开始下载:"+names[i])

imglinks=getImgs(link)

download(link,imglinks,dir_name + './' + str(names[i]))

print(names[i]+"下载完毕")

print("休息一会儿,稍微继续下载下一章")

time.sleep(10)

print("————————————————————————————————————————————————————————————————————————————————")

print(manhuas+"已经完全下载完毕")

主函数入口

if __name__ == '__main__':

main()

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394

外汇MT4教程https://www.kaifx.cn/mt4.html

老哥的代码:

import requests

import os

import re

from bs4 import BeautifulSoup

from contextlib import closing

from tqdm import tqdm

import time

# 创建保存目录

save_dir = '妖神记'

if save_dir not in os.listdir('./'):

os.mkdir(save_dir)

target_url = "https://www.dmzj.com/info/yaoshenji.html"

# 获取动漫章节链接和章节名

r = requests.get(url = target_url)

bs = BeautifulSoup(r.text, 'lxml')

list_con_li = bs.find('ul', class_="list_con_li")

cartoon_list = list_con_li.find_all('a')

chapter_names = []

chapter_urls = []

for cartoon in cartoon_list:

href = cartoon.get('href')

name = cartoon.text

chapter_names.insert(0, name)

chapter_urls.insert(0, href)

# 下载漫画

for i, url in enumerate(tqdm(chapter_urls)):

download_header = {

'Referer': url

}

name = chapter_names[i]

# 去掉.

while '.' in name:

name = name.replace('.', '')

chapter_save_dir = os.path.join(save_dir, name)

if name not in os.listdir(save_dir):

os.mkdir(chapter_save_dir)

r = requests.get(url = url)

html = BeautifulSoup(r.text, 'lxml')

script_info = html.script

pics = re.findall('\d{13,14}', str(script_info))

for j, pic in enumerate(pics):

if len(pic) == 13:

pics[j] = pic + '0'

pics = sorted(pics, key=lambda x:int(x))

chapterpic_hou = re.findall('\|(\d{5})\|', str(script_info))[0]

chapterpic_qian = re.findall('\|(\d{4})\|', str(script_info))[0]

for idx, pic in enumerate(pics):

if pic[-1] == '0':

url = 'https://images.dmzj.com/img/c...' + chapterpic_qian + '/' + chapterpic_hou + '/' + pic[:-1] + '.jpg'

else:

url = 'https://images.dmzj.com/img/c...' + chapterpic_qian + '/' + chapterpic_hou + '/' + pic + '.jpg'

pic_name = '%03d.jpg' % (idx + 1)

pic_save_path = os.path.join(chapter_save_dir, pic_name)

with closing(requests.get(url, headers = download_header, stream = True)) as response:

chunk_size = 1024

content_size = int(response.headers['content-length'])

if response.status_code == 200:

with open(pic_save_path, "wb") as file:

for data in response.iter_content(chunk_size=chunk_size):

file.write(data)

else:

print('链接异常')

time.sleep(10)

python 主程序入口_python爬几部国漫相关推荐

  1. python做壁纸_Python爬取壁纸

    不想一张张看壁纸怎么办,不想一张张下载怎么办,来让我们用python解决一切,爬取一网站所有壁纸. 1.准备前期运行环境 ·python运行环境,安装request模块 (这个问题需要自己去解决) 2 ...

  2. 听说国漫最近崛起了,那我们就来爬几部国漫看看(动态加载,反爬)

    长按点赞,等你来干!!! 目录 1.前言 2.反爬过程 2.1基本思路 2.2爬取章节链接 2.3爬取漫画链接 2.3.1无法查看源码 2.3.2动态加载 2.3.3漫画乱序 2.3.4下载漫画报40 ...

  3. python爬虫数据_python爬取数据分析

    一.python爬虫使用的模块 1.import requests 2.from bs4 import BeautifulSoup 3.pandas 数据分析高级接口模块 二. 爬取数据在第一个请求中 ...

  4. python京东商品_Python爬取京东的商品分类与链接

    前言 本文主要的知识点是使用Python的BeautifulSoup进行多层的遍历. 如图所示.只是一个简单的哈,不是爬取里面的隐藏的东西. 示例代码 from bs4 import Beautifu ...

  5. 杭州python爬虫招聘_python爬取招聘网站(智联,拉钩,Boss直聘)

    刚好最近有这需求,动手写了几个 就贴上代码算了 1.智联 将结果保存为python的一个数据框中 import requests from requests.exceptions import Req ...

  6. python 行情数据_python爬取期权行情数据

    想要弄点数据要求高的可以找收费的数据服务商,例如wind,东方财富,后者便宜点,tushare也提供了期权行情数据,但是门槛是有积分限制,其他的地方只能爬取了.做期权策略分析没有数据怎么行,没钱就写一 ...

  7. python 简书_python爬取简书网文章的方法

    python爬取简书网文章的方法 发布时间:2020-06-30 14:37:08 来源:亿速云 阅读:100 作者:清晨 这篇文章主要介绍python爬取简书网文章的方法,文中示例代码介绍的非常详细 ...

  8. python二手房数据分析_Python 爬取北京二手房数据,分析北漂族买得起房吗? | 附完整源码...

    作者 徐麟 本文经授权转自公众号数据森麟(ID: shujusenlin) 房价高是北漂们一直关心的话题,本文就对北京的二手房数据进行了分析. 本文主要分为两部分:Python爬取赶集网北京二手房数据 ...

  9. python专用壁纸_Python爬Mac专用壁纸

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 很多人都知道,网络上有很多的壁纸,但是大部分都是1980*1080的分辨率,可是这专为Mac系统做的壁纸确实5000+*3000+的分辨率,你可以想象一下 ...

最新文章

  1. 优秀开发者必备技能包:Python调试器
  2. pandas 绘图 机器学习看特征相关性
  3. MFC 单文档的全局变量
  4. 一次 Java 内存泄漏排查过程,学习学习
  5. hihocoder #1078 : 线段树的区间修改
  6. ios程序 调试log宏的添加
  7. Java实现统计某字符串在另一个字符串中出现的次数
  8. leetcode刷题:1.无重复字符的最长字串
  9. 沃尔玛宣布与TikTok达成直播带货合作?
  10. linux目录/etc/nc.d/nc.local开机启动项无效
  11. 用java编写录音机类_java实现录音机
  12. DB2 SQLCODE 异常大全编辑(三)
  13. python 爬取百度日历
  14. python websocket服务器端_python实现websocket服务器
  15. JS~Boxy和JS模版实现一个标准的消息提示框
  16. cadence 通孔焊盘_Allegro PCB -通孔焊盘制作 及Flash制作
  17. 中级php工程师笔试,PHP工程师笔试题目及行测题型示例
  18. HTML的图文排版,css 文章内容排版实例
  19. java 中 Native.loadLibrary 不能加载 jar 包中库文件的解决方案
  20. 【7047】北京游:知乎、百度、豆瓣、新浪微博

热门文章

  1. getchar()的用法详细讲解
  2. C#学习笔记:总结提升
  3. jekyll个人博客中添加音乐播放插件
  4. @component的作用详细介绍
  5. Mac OS 卸载 betterzip 后,右键还是有 “使用betterzip解压/压缩
  6. php+jq+添加css,jQuery添加/改变/移除CSS类
  7. Android:简单音乐播放器,实现歌曲列表显示,播放暂停,切歌等功能
  8. matlab 里temp,temp _U在matlab里是什么意思
  9. 新建了一个高质量技术交流群
  10. 驱赶蚊子、蚂蚁、蟑螂的妙法