温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python加js解密怎么爬取漫画

发布时间:2021-11-25 13:48:41 来源:亿速云 阅读:146 作者:iii 栏目:大数据

本篇内容介绍了“Python加js解密怎么爬取漫画”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

通过审查元素我们可以发现,所有的章节链接都是通过一个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

“Python加js解密怎么爬取漫画”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI