溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python加js解密怎么爬取漫畫

發(fā)布時間:2021-11-25 13:48:41 來源:億速云 閱讀:155 作者:iii 欄目:大數(shù)據(jù)

本篇內容介紹了“Python加js解密怎么爬取漫畫”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

通過審查元素我們可以發(fā)現(xiàn),所有的章節(jié)鏈接都是通過一個ol的標簽進行包裹,所以我們只要獲取要頁面中的一個ol標簽下,所有的a鏈接就可以成功的獲取到所有的章節(jié)鏈接了。

代碼如下:

#獲取漫畫的章節(jié)地址
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)
    # 找到所有章節(jié)列表
    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

獲取到漫畫的所有章節(jié)后,我們可以通過請求每一章節(jié)的鏈接,來獲取頁面的具體信息,代碼如下:

# 請求漫畫每一章節(jié)的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)

  # 分析數(shù)據(jù)并下載對應章節(jié)圖片
  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下面的數(shù)據(jù)并下載就可以了。代碼如下:

# js解碼獲取章節(jié)信息
    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運行,進行數(shù)據(jù)解碼
        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解碼獲取章節(jié)信息
    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運行,進行數(shù)據(jù)解碼
        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

“Python加js解密怎么爬取漫畫”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網(wǎng)站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI