本文的文字及圖片來源于網路,僅供學習、交流使用,不具有任何商業用途,著作權歸原作者所有,如有問題請及時聯系我們以作處理,
以下文章來源于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 = https://www.cnblogs.com/hhh188764/archive/2020/09/24/re.findall("(?<=var DATA = 'https://www.cnblogs.com/hhh188764/archive/2020/09/24/).*?(?=')", 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 = https://www.cnblogs.com/hhh188764/archive/2020/09/24/re.findall("(?<=var DATA = 'https://www.cnblogs.com/hhh188764/archive/2020/09/24/).*?(?=')", 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/121097.html
標籤:其他
