本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。
以下文章来源于Python爬虫案例,作者麦自香
转载地址
https://blog.csdn.net/fei347795790?t=1
在上一篇文章我们既然拿到了页面返回的值后,接下来的操作也就简单了。
本次我们是通过漫画的id进行漫画爬取,所以我们不需要再做搜索的那种形式了,若是有兴趣的话可以参照~上一篇文章~,此处直接让我们进入到漫画的章节页面。
通过审查元素我们可以发现,所有的章节链接都是通过一个ol的标签进行包裹,所以我们只要获取要页面中的一个ol标签下,所有的a链接就可以成功的获取到所有的章节链接了。
代码如下:
#获取漫画的章节地址
def get_chapter_info(self):
chapter_info = {}
url = 'http://ac.qq.com/Comic/ComicInfo/id/{}'.format(self.comic_id)
html_text = self.get_html(url)
html = self.parser(html_text)
# 找到所有章节列表
ol = html.find('ol')[0]
chapters = ol.find('a')
index = 0
for chapter in chapters:
title = chapter.attrs['title']
link = parse.urljoin(TxComic.COMIC_HOST, chapter.attrs['href'])
key = '第{}章'.format(index)
chapter_info[key] = {'title': title, 'link': link}
index += 1
return chapter_info
获取到漫画的所有章节后,我们可以通过请求每一章节的链接,来获取页面的具体信息,代码如下:
# 请求漫画每一章节的url链接
def get_comic_info(self):
chapters = self.get_chapter_info()
# print(chapters)
for k, v in chapters.items():
url = v['link']
pics = self.get_chapter_pics(url)
self.async_data(pics)
# 分析数据并下载对应章节图片
def async_data(self, res_data):
book_name = res_data['comic']['title']
if not os.path.exists(book_name):
os.mkdir(book_name)
chapter_tname = "第" + str(res_data['chapter']['cid']) + '章__' + res_data['chapter']['cTitle']
chapter_name = eval(repr(chapter_tname).replace('/', '@'))
# print(chapter_name)
path = os.path.join(book_name, chapter_name)
if not os.path.exists(path):
os.mkdir(path)
# print(res_data['picture'])
for index, v in enumerate(res_data['picture']):
name = os.path.join(path, "{}.png".format(index))
self.download_img(name, v['url'])
print(chapter_name + "爬取完毕")
在此处我们可以成功的请求到每一个url链接,接下来我们只需要对返回的页面进行js解密,然后取出_v下面的数据并下载就可以了。代码如下:
# js解码获取章节信息
def get_chapter_pics(slef, url):
while True:
try:
response = requests.get(url).text
# 获取W['DA' + 'TA']
data = re.findall("(?<=var DATA = ').*?(?=')", response)[0]
nonce = re.findall('window\[".+?(?<=;)', response)[0]
nonce = '='.join(nonce.split('=')[1:])[:-1]
# 获取W['n' + 'onc' + 'e']
nonce = execjs.eval(nonce)
break
except:
pass
# 模拟js运行,进行数据解码
T = list(data)
N = re.findall('\d+[a-zA-Z]+', nonce)
jlen = len(N)
while jlen:
jlen -= 1
jlocate = int(re.findall('\d+', N[jlen])[0]) & 255
jstr = re.sub('\d+', '', N[jlen])
del T[jlocate:jlocate + len(jstr)]
T = ''.join(T)
keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
a = []
e = 0
while e < len(T):
b = keyStr.index(T[e])
e += 1
d = keyStr.index(T[e])
e += 1
f = keyStr.index(T[e])
e += 1
g = keyStr.index(T[e])
e += 1
b = b << 2 | d >> 4
d = (d & 15) << 4 | f >> 2
h = (f & 3) << 6 | g
a.append(b)
if 64 != f:
a.append(d)
if 64 != g:
a.append(h)
_v = json.loads(bytes(a))
return _v
代码整合如下:
# js解码获取章节信息
def get_chapter_pics(slef, url):
while True:
try:
response = requests.get(url).text
# 获取W['DA' + 'TA']
data = re.findall("(?<=var DATA = ').*?(?=')", response)[0]
nonce = re.findall('window\[".+?(?<=;)', response)[0]
nonce = '='.join(nonce.split('=')[1:])[:-1]
# 获取W['n' + 'onc' + 'e']
nonce = execjs.eval(nonce)
break
except:
pass
# 模拟js运行,进行数据解码
T = list(data)
N = re.findall('\d+[a-zA-Z]+', nonce)
jlen = len(N)
while jlen:
jlen -= 1
jlocate = int(re.findall('\d+', N[jlen])[0]) & 255
jstr = re.sub('\d+', '', N[jlen])
del T[jlocate:jlocate + len(jstr)]
T = ''.join(T)
keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
a = []
e = 0
while e < len(T):
b = keyStr.index(T[e])
e += 1
d = keyStr.index(T[e])
e += 1
f = keyStr.index(T[e])
e += 1
g = keyStr.index(T[e])
e += 1
b = b << 2 | d >> 4
d = (d & 15) << 4 | f >> 2
h = (f & 3) << 6 | g
a.append(b)
if 64 != f:
a.append(d)
if 64 != g:
a.append(h)
_v = json.loads(bytes(a))
return _v