鱼C论坛

 找回密码
 立即注册
查看: 2246|回复: 0

[作品展示] 动漫迷有福利啦,这个帖子教会你爬取腾讯动漫

[复制链接]
发表于 2020-3-20 18:12:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 2434849827 于 2020-3-21 09:51 编辑

今天弄了一上午爬取腾讯动漫,大厂的就是难爬一点,各种加密,抓了一上午的包都没抓到我要的js文件,然后我找了半天资料,发现用selenium这个模拟浏览器直接去找到动漫图片的网址,然后用request下载会比较简单,代码量比较少,但是里面涵盖的知识点比较多,给大家提供参考 3.png 2.png 1.png
  1. [color=YellowGreen]import os
  2. import time,re
  3. import requests
  4. from selenium import webdriver

  5. def getChapterUrl():
  6.     headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36"}
  7.     name = input('请输入要下载的动漫名称:')
  8.     url2 = f'https://ac.qq.com/Comic/searchList?search={name}'
  9.     html2 = requests.get(url = url2,headers = headers)
  10.     r11 = re.findall(r'<a href="/Comic/comicInfo/id/(\d{4,})" title',html2.text)
  11.     res = requests.get(url = f'https://ac.qq.com/Comic/comicInfo/id/{r11[0]}', headers=headers)
  12.     r1 = re.findall(r'title="(.*?):\d{1,5}..*"',res.text)
  13.     r2 = re.findall(r'href="(/ComicView/index/id/.*/cid/.*?)"',res.text)
  14.     r3 = re.findall(r'title=".*?:\d{1,5}.([\u4e00-\u9fa5].*?)"', res.text)
  15.     del r2[-1]
  16.     url_1 = []
  17.     #print(r3)
  18.     for i in r2:
  19.         url_1.append('https://ac.qq.com'+i)
  20.     n = 0
  21.     for i in url_1:
  22.         getChapterFile(i, r1[n], r3[n])
  23.         n += 1

  24. def getChapterFile(url,path1,path2):
  25.     # 漫画名称目录
  26.     print(url)
  27.     path=os.path.join(path1)
  28.     if not os.path.exists(path):
  29.         os.mkdir(path)
  30.     # 章节目录
  31.     path=path+'\\'+path2
  32.     if not os.path.exists(path):
  33.         os.mkdir(path)
  34.     chrome = webdriver.Chrome()
  35.     chrome.get(url)
  36.     time.sleep(4)
  37.     imgs = chrome.find_elements_by_xpath("//div[@id='mainView']/ul[@id='comicContain']//img")
  38.     for i in range(0, len(imgs)):
  39.             js="document.getElementById('mainView').scrollTop="+str((i) * 1280)
  40.             chrome.execute_script(js)
  41.             time.sleep(3)
  42.             r = re.findall(r'https://manhua.qpic.cn',imgs[i].get_attribute("src"))
  43.             if r:
  44.                 print(imgs[i].get_attribute("src"))
  45.                 with open(path+'\\'+str(i)+'.png', 'wb') as f:
  46.                     f.write(requests.get(imgs[i].get_attribute("src")).content)
  47.     chrome.close()
  48.     print('下载完成')

  49. if __name__ == '__main__':
  50.     getChapterUrl()
  51. [/color]
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-9 04:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表