鱼C论坛

 找回密码
 立即注册
查看: 1736|回复: 4

[作品展示] 重出江湖!明日方舟wiki资料下载器

[复制链接]
发表于 2023-7-13 12:09:00 | 显示全部楼层 |阅读模式

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

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

x
代码如下:
  1. from selenium import webdriver
  2. import requests
  3. import urllib
  4. import os
  5. from bs4 import BeautifulSoup
  6. import time
  7. import threading

  8. pre_url = "https://prts.wiki"
  9. PATH = os.getcwd()+"\\Arknights\"
  10. headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0"}
  11. print(PATH)
  12. #动态提取网页源代码
  13. def get_res(url):
  14.     option=webdriver.ChromeOptions()
  15.     #隐藏chrome窗口
  16.     option.add_argument('--headless')
  17.     option.add_argument("--disable-infobars")
  18.     browser = webdriver.Chrome(options=option)
  19.     browser.get(url)
  20.     #缓冲
  21.     time.sleep(5)
  22.     #下拉滑动条
  23.     browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
  24.     time.sleep(1)
  25.     res=browser.page_source
  26.     browser.quit()
  27.     return res

  28. #下载立绘
  29. def ins_pic(html):
  30.     main_path = PATH + '立绘\\'
  31.     soup=BeautifulSoup(html,'html.parser')
  32.     characters=soup.find('div',{'id':'filter-data'}).find_all('div')
  33.     for c in characters:
  34.         character_name=c.get('data-zh')
  35.         path=main_path+character_name
  36.         if not os.path.exists(path):
  37.             os.makedirs(path)
  38.         character_urls=[pre_url+'/w/文件:立绘_'+character_name+'_1.png',pre_url+'/w/文件:立绘_'+character_name+'_2.png']
  39.         _soup=BeautifulSoup(requests.get(character_urls[1]).text,'html.parser')
  40.         if(_soup.find('div',{'class':'fullImageLink'})==None):
  41.             del _soup
  42.             del character_urls[1]
  43.         for i in range(len(character_urls)):
  44.             res=BeautifulSoup(requests.get(character_urls[i]).text,'html.parser').find('div',{'class':'fullImageLink'}).find('a')
  45.             res=pre_url+res.get('href')
  46.             res=requests.get(res)
  47.             print('正在爬取:'+character_name+'_'+str(i+1)+'.png')
  48.             with open(path+'\\'+character_name+'_'+str(i+1)+'.png','wb') as f:
  49.                 f.write(res.content)
  50. #下载皮肤
  51. def get_skin(url):
  52.     path = PATH + '立绘\\'
  53.     if not os.path.exists(path):
  54.         os.makedirs(path)
  55.     soup = BeautifulSoup(requests.get(url).text,'html.parser')
  56.     pic_urls=soup.find_all('table',{'class':'wikitable logo nomobile'})
  57.     for pic in pic_urls:
  58.         try:
  59.             url=pic.find_all('a',{'class':'image'})[1]
  60.             img=url.find('img')
  61.             name=str(img.get('alt'))
  62.             name=name[name.find('立绘')+3:]
  63.             print('正在爬取:'+name)
  64.             file_name=name[:name.find(' skin')]
  65.             if not os.path.exists(path+file_name):
  66.                 os.makedirs(path+file_name)
  67.             res=str(url.get('href'))
  68.             res=pre_url+str(BeautifulSoup(requests.get(pre_url+res).text,'html.parser').find('div',{'class':'fullImageLink'}).find('a').get('href'))
  69.             res=requests.get(res)
  70.             with open(path+'\\'+file_name+'\\'+name,'wb') as f:
  71.                 f.write(res.content)
  72.             print(name+'爬取完毕')
  73.         except Exception as e:
  74.             print("爬取图片出错:", str(e))
  75. #下载语音记录
  76. def download_audio(num,url,file_name,save_path,if_again):
  77.     try:
  78.         temp=requests.get(url,headers=headers)
  79.         if(str(temp).find('403')!=-1):
  80.             print('403 Forbidden:'+url)
  81.             return
  82.         if(str(temp.content).find('No')!=-1):
  83.             print('语音不存在',url)
  84.             return
  85.         with open(save_path+'\\'+file_name+'.wav','wb') as f:
  86.             f.write(temp.content)
  87.         print("爬取完毕")                        
  88.     except Exception as e:
  89.         print("程序运行出错:", str(e))
  90.         flag=-1
  91.         while(flag!='1' and flag!='0'):
  92.             flag=input('是否再次尝试(1:是/0:否)')
  93.         if(flag=='1'):
  94.             download_audio(num,url)
  95. def ins_audio(html,if_again):
  96.     print('开始爬取')
  97.     soup=BeautifulSoup(html,'html.parser')
  98.     characters=soup.find('div',{'id':'filter-data'}).find_all('div')
  99.     for c in characters:
  100.         character_name=c.get('data-zh')
  101.         if os.path.exists(PATH+'语音\\'+character_name) and if_again=='0':
  102.             print('跳过')
  103.             continue
  104.         path=PATH+'语音\\'+character_name+'\\'
  105.         url=pre_url+'/w/'+character_name+'/语音记录'
  106.         res=BeautifulSoup(get_res(url),'html.parser')
  107.         print('已经获得'+character_name+'语音链接,开始下载')
  108.         try:
  109.             divs=res.find('div',{'class':'table-row-group'}).find_all('div',{'class':'table-row'})
  110.             for div in divs:
  111.                 audio_name=div.find('div',{'class':'table-cell text-center font-bold p-1 !bg-table border border-solid border-divider align-middle truncate'})
  112.                 audio_name=audio_name.text
  113.                 save_path=path+audio_name
  114.                 if not os.path.exists(save_path):
  115.                     os.makedirs(save_path)
  116.                 tmp=div.find('a')
  117.                 tmp='https:'+str(tmp.get('href'))
  118.                 print(tmp)
  119.                 file_names=['日本語','中文','한국어','English']
  120.                 urls=['jp','cn','kr','en']
  121.                 #网址格式化
  122.                 if(tmp.find('/voice/')==-1):
  123.                     urls[0]=tmp[:tmp.find('/voice')]+'/voice/'+tmp[tmp.find('/voice')+10:]
  124.                     urls[1]=tmp[:tmp.find('/voice')]+'/voice_cn/'+tmp[tmp.find('/voice')+10:]
  125.                     urls[2]=tmp[:tmp.find('/voice')]+'/voice_kr/'+tmp[tmp.find('/voice')+10:]
  126.                     urls[3]=tmp[:tmp.find('/voice')]+'/voice_en/'+tmp[tmp.find('/voice')+10:]
  127.                 else:
  128.                     urls[0]=tmp
  129.                     urls[1]=tmp[:tmp.find('/voice')]+'/voice_cn/'+tmp[tmp.find('/voice')+7:]
  130.                     urls[2]=tmp[:tmp.find('/voice')]+'/voice_kr/'+tmp[tmp.find('/voice')+7:]
  131.                     urls[3]=tmp[:tmp.find('/voice')]+'/voice_en/'+tmp[tmp.find('/voice')+7:]
  132.                 print('正在下载'+character_name+'-'+audio_name+'语音\n')
  133.                 for i in range(len(urls)):
  134.                     download_audio(i,urls[i],file_names[i],save_path,if_again)
  135.                            
  136.         except Exception as e:
  137.             print("程序运行出错:", str(e))
  138. #爬取bgm
  139. def ins_music(home_url):
  140.     path=PATH+'背景音乐'
  141.     if not os.path.exists(path):
  142.         os.makedirs(path)
  143.     print('获取音乐网址')
  144.     soup=BeautifulSoup(requests.get(home_url).text,'html.parser')
  145.     tmp_urls=soup.find_all('table',{'class':"wikitable mw-collapsible mw-collapsible-dark mw-collapsible-title-center mw-collapsed"})
  146.     bgms=[]
  147.     for tmp in tmp_urls:
  148.         bgms+=tmp.find_all('tr')
  149.     i=0
  150.     while(i<len(bgms)):
  151.         if(bgms[i].find('source')==None):
  152.             del bgms[i]
  153.             continue
  154.         i+=1
  155.     print("获取完毕")
  156.     for bgm in bgms:
  157.         bgm_name=bgm.find('div',{'class':'nodesktop'}).text
  158.         while ':' in bgm_name:
  159.             bgm_name=bgm_name[:bgm_name.find(':')]+':'+bgm_name[bgm_name.find(':')+1:]
  160.         while '<' in bgm_name:
  161.             bgm_name=bgm_name[:bgm_name.find('<')]+'《'+bgm_name[bgm_name.find('<')+1:]
  162.         while '>' in bgm_name:
  163.             bgm_name=bgm_name[:bgm_name.find('>')]+'》'+bgm_name[bgm_name.find('>')+1:]
  164.         while '?' in bgm_name:
  165.             bgm_name=bgm_name[:bgm_name.find('?')]+'?'+bgm_name[bgm_name.find('?')+1:]
  166.         while '|' in bgm_name:
  167.             bgm_name=bgm_name[:bgm_name.find('|')]+'-'+bgm_name[bgm_name.find('|')+1:]
  168.         while '*' in bgm_name:
  169.             bgm_name=bgm_name[:bgm_name.find('*')]+'×'+bgm_name[bgm_name.find('*')+1:]
  170.         while '/' in bgm_name:
  171.             bgm_name=bgm_name[:bgm_name.find('/')]+'-'+bgm_name[bgm_name.find('/')+1:]
  172.         while '\\' in bgm_name:
  173.             bgm_name=bgm_name[:bgm_name.find('\\')]+'-'+bgm_name[bgm_name.find('\\')+1:]
  174.         bgm_url=bgm.find('source')
  175.         bgm_url=bgm_url.get('src')
  176.         print("正在爬取"+bgm_name)
  177.         with open(path+'\\'+bgm_name+'.mp3','wb') as f:
  178.             f.write(requests.get(bgm_url,headers=headers).content)
  179.         print("完成")
  180. #主函数
  181. def main_code():
  182.     try:
  183.         tmp1=-1
  184.         while(tmp1!='1' and tmp1!='0'):
  185.             tmp1=input('是否爬取角色语音(1:是 0:否):')
  186.         tmp2=-1
  187.         while(tmp2!='1' and tmp2!='0'):
  188.             tmp2=input('是否爬取角色立绘与皮肤(1:是 0:否):')
  189.         tmp3=-1
  190.         while(tmp3!='1' and tmp3!='0'):
  191.             tmp3=input('是否爬取游戏公测版本BGM及在线发布的游戏相关曲目(1:是 0:否):')
  192.         if int(tmp1)==1:
  193.             if_again=-1
  194.             while(if_again!='0' and if_again!='1'):
  195.                 if_again=input('是否重复下载(1:是/0:否)')
  196.             html = get_res('http://prts.wiki/w/%E5%B9%B2%E5%91%98%E4%B8%80%E8%A7%88')
  197.             ins_audio(html,if_again)
  198.             print("爬取语音函数运行完毕, 请于文件夹中查看\n")
  199.         if int(tmp2)==1:
  200.             ins_pic(get_res('http://prts.wiki/w/%E5%B9%B2%E5%91%98%E4%B8%80%E8%A7%88'))
  201.             html = get_res('https://prts.wiki/w/%E6%97%B6%E8%A3%85%E5%9B%9E%E5%BB%8A')
  202.             soup=BeautifulSoup(html,'html.parser')
  203.             urls=soup.find_all('div',{'class':'skinwrapper charskinbtn-controler'})
  204.             for i in range(len(urls)):
  205.                 urls[i]=str(urls[i].find('a').get('href'))
  206.                 urls[i]=pre_url+urls[i][:urls[i].find('#')]
  207.             urls=list(set(urls))
  208.             for url in urls:
  209.                 get_skin(url)
  210.         
  211.         if tmp3=='1':
  212.             ins_music('https://prts.wiki/w/%E9%9F%B3%E4%B9%90%E9%89%B4%E8%B5%8F')
  213.             print("爬取游戏公测版本BGM及在线发布的游戏相关曲目函数运行完毕, 请于文件夹中查看\n")

  214.     except Exception as e:
  215.         print("程序运行出错:", str(e))
  216.    

  217. if __name__ == "__main__":
  218.     main_code()
复制代码

评分

参与人数 1荣誉 +5 贡献 +3 收起 理由
歌者文明清理员 + 5 + 3

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-7-13 12:09:43 | 显示全部楼层
有打包成exe,但文件太大了
……qwq
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-13 12:11:28 | 显示全部楼层
chenyiyun 发表于 2023-7-13 12:09
有打包成exe,但文件太大了
……qwq

网盘
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-7-13 14:10:41 | 显示全部楼层

太麻烦了,平时都不咋用……
给个邮箱我发你吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-13 14:21:53 | 显示全部楼层
chenyiyun 发表于 2023-7-13 14:10
太麻烦了,平时都不咋用……
给个邮箱我发你吧

不用,pyinstaller我会
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-27 15:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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