鱼C论坛

 找回密码
 立即注册
查看: 4340|回复: 1

[技术交流] 爬取明日方舟立绘

[复制链接]
发表于 2021-3-10 09:25:16 | 显示全部楼层 |阅读模式

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

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

x
代码写得很烂,看看就行


爬虫基于明日方舟的Wiki:http://prts.wiki/w/%E9%A6%96%E9%A1%B5

  1. import requests
  2. import urllib
  3. import os
  4. from bs4 import BeautifulSoup

  5. pre_url = "http://prts.wiki/w/"
  6. PATH = os.getcwd()+"\\Arknights\"
  7. print(PATH)

  8. def get_res(url):
  9.     res = requests.get(url)
  10.     if not res:
  11.         print(url, "is not a correct url;")
  12.     return res


  13. def get_names(html):
  14.     soup = BeautifulSoup(html.text, 'html.parser')
  15.     _targets = soup.find_all('div', class_='smwdata')
  16.     names = []
  17.     for each in _targets:
  18.         each = each.get("data-cn")
  19.         if each:
  20.             names.append(each.strip())
  21.     return names


  22. def get_url(name):
  23.     url = pre_url + urllib.parse.quote(name)
  24.     print("正在爬取", name, "的网页,网址为:", url)
  25.     return url


  26. def pic_url(url):
  27.     html = get_res(url)
  28.     soup = BeautifulSoup(html.text, 'html.parser')
  29.     images = []
  30.     for each in soup.find_all('div', id='charimg'):
  31.         pic1 = each.find('div', id='img-stage1')
  32.         pic2 = each.find('div', id='img-stage2')
  33.         if pic1:
  34.             img1 = pic1.find('img').get('src')
  35.             images.append(img1)
  36.         if pic2:
  37.             img2 = pic2.find('img').get('src')
  38.             if img2:
  39.                 images.append(img2)
  40.     if images:
  41.         print("成功从网页中找到图片链接!")
  42.     else:
  43.         print("ERROR!!!in", url)
  44.     return images


  45. def ins_png(urls, name):
  46.     path = PATH + '立绘\\'
  47.     if not os.path.exists(path):
  48.         os.makedirs(path)
  49.     for i in range(len(urls)):
  50.         res = requests.get(urls[i])
  51.         if res:
  52.             if os.path.exists(path+name+'_'+str(i+1)+'.png'):
  53.                 return
  54.             img = open(path+name+'_'+str(i+1)+'.png', 'wb')
  55.             img.write(res.content)
  56.             print(name, i+1, "已经保存到", path)
  57.             img.close()


  58. def main_code(home_url):
  59.     html = get_res(home_url)
  60.     names = get_names(html)
  61.     for name in names:
  62.         url = get_url(name)
  63.         pics = pic_url(url)
  64.         ins_png(pics, name)
  65.     print("爬取皮肤函数运行完毕, 请于文件夹中查看\n")


  66. if __name__ == "__main__":
  67.     url0 = "http://prts.wiki/w/%E5%B9%B2%E5%91%98%E4%B8%80%E8%A7%88"
  68.     main_code(url0)
复制代码

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

使用道具 举报

发表于 2021-3-12 13:09:52 | 显示全部楼层
瞅瞅
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-26 17:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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