鱼C论坛

 找回密码
 立即注册
查看: 1151|回复: 2

[已解决]pyinstaller打包后问题

[复制链接]
发表于 2021-2-22 16:17:06 | 显示全部楼层 |阅读模式

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

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

x
搞了个B站弹幕词云生成,用pyinstaller 打包后提示如下错误 :
1.png

下面是我写的代码:
  1. import requests
  2. import wordcloud
  3. import re
  4. import pprint
  5. from tkinter import *

  6. video_url = "https://www.bilibili.com/video/BV1ep4y1s7xG?t=66"


  7. headers={'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
  8.           'referer':'https://www.bilibili.com',
  9.           'cookie': "_uuid=6185ADAD-E351-4BC4-766E-0B38AF97020786236infoc; buvid3=4990DC29-6DB5-40B2-A10B-6EB9D9AF8F2B155832infoc; sid=d80bw00y; rpdid=|(umRkJu|m~)0J'ulmJl~JuY|; CURRENT_QUALITY=80; LIVE_BUVID=AUTO6915954949231230; blackside_state=1; CURRENT_FNVAL=80; fingerprint=98c99d00d54fe004b372b6bda52ab256; buvid_fp=4990DC29-6DB5-40B2-A10B-6EB9D9AF8F2B155832infoc; buvid_fp_plain=4990DC29-6DB5-40B2-A10B-6EB9D9AF8F2B155832infoc; DedeUserID=431943900; DedeUserID__ckMd5=cd3a9705c2aaafad; SESSDATA=e0703c30%2C1629442221%2Cf30c4*21; bili_jct=64f53090c52cf878f430e6cb30d8c2fb; bsource=search_360; finger=1571944565; bp_video_offset_431943900=494159708514409608; PVID=3"}




  10. def openVideoUrl(url,headers):
  11.     '''
  12.     打开视频网页,获取网页原代码并返回
  13.     '''
  14.     html = requests.get(url=url,headers=headers).text
  15.     return html

  16. def findCid(html):
  17.     '''
  18.     从网页源代码中找到cid并返回
  19.     '''
  20.     p = re.compile('cid=[0-9]*')
  21.     cid = p.findall(html)[0].split('=')[1]
  22.     return cid

  23. def openBarrageUrl(cid,headers):
  24.     '''
  25.      打开弹幕网址,提取弹幕

  26.      cid:cid 号
  27.      
  28.      headers: 请求头
  29.     '''
  30.     barrage_url = "https://comment.bilibili.com/{}.xml".format(cid)
  31.     html = requests.get(url=barrage_url,headers=headers)
  32.     xml = html.content.decode('utf-8')
  33.    
  34.     with open(r"弹幕.xml",'w',encoding='utf-8') as f:
  35.         f.write(xml)
  36.     p = re.compile('[\u4e00-\u9fa5]+')
  37.     #pprint.pprint(xml)
  38.     #print(p.findall(xml))
  39.     return str(p.findall(xml))
  40.    

  41. def genWordCloud(dm_text):
  42.     '''
  43.     生成词云图

  44.     dm_text:字符串
  45.     '''
  46.     stop_words = {"'"}
  47.     wc = wordcloud.WordCloud(font_path=r"C:\Windows\Fonts\simhei.ttf",width=800,height=600,stopwords=stop_words)
  48.     wc.generate(dm_text)
  49.     image = wc.to_image()
  50.     image.show()

  51. def showtime():
  52.     AV_url = input_lable.get()
  53.     html = openVideoUrl(url=AV_url,headers=headers)
  54.     cid = findCid(html=html)
  55.     dm_data = openBarrageUrl(cid=cid,headers=headers)
  56.     genWordCloud(dm_data)
  57.    
  58. if __name__ == "__main__":
  59.     window = Tk()
  60.     window.title("bilibili视频弹幕词云图生成器")
  61.     window.resizable(0,0)
  62.     input_lable = Entry(window,width = '25',fg = 'white',bg = 'skyblue')
  63.     input_lable.pack()
  64.     input_lable.insert(0,"输入视频网址")
  65.     button = Button(window,text = "生成词云图片",foreground='blue',command = showtime)
  66.     button.pack()
  67.     window.mainloop()
复制代码
最佳答案
2021-2-22 17:16:42
出现这种报错的多数是因为相对路径的问题,打包后的程序,相对路径起始点并不在exe所在的文件夹,而是系统临时文件夹内。
试试以下改动打包后是否能运行,我手机写的测试不了(15~19行,51行,如果源码中还有其他涉及相对路径的地方也像51行一样改动):
  1. import os
  2. import requests
  3. import wordcloud
  4. import re
  5. import pprint
  6. from tkinter import *

  7. video_url = "https://www.bilibili.com/video/BV1ep4y1s7xG?t=66"


  8. headers={'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
  9.           'referer':'https://www.bilibili.com',
  10.           'cookie': "_uuid=6185ADAD-E351-4BC4-766E-0B38AF97020786236infoc; buvid3=4990DC29-6DB5-40B2-A10B-6EB9D9AF8F2B155832infoc; sid=d80bw00y; rpdid=|(umRkJu|m~)0J'ulmJl~JuY|; CURRENT_QUALITY=80; LIVE_BUVID=AUTO6915954949231230; blackside_state=1; CURRENT_FNVAL=80; fingerprint=98c99d00d54fe004b372b6bda52ab256; buvid_fp=4990DC29-6DB5-40B2-A10B-6EB9D9AF8F2B155832infoc; buvid_fp_plain=4990DC29-6DB5-40B2-A10B-6EB9D9AF8F2B155832infoc; DedeUserID=431943900; DedeUserID__ckMd5=cd3a9705c2aaafad; SESSDATA=e0703c30%2C1629442221%2Cf30c4*21; bili_jct=64f53090c52cf878f430e6cb30d8c2fb; bsource=search_360; finger=1571944565; bp_video_offset_431943900=494159708514409608; PVID=3"}
  11.          
  12. _rel_origin = os.path.dirname(os.path.abspath(__file__))

  13. def relpath(s):
  14.     """涉及相对路径的地方,套用此函数来获取绝对路径"""
  15.     return os.path.join(_rel_origin, s)




  16. def openVideoUrl(url,headers):
  17.     '''
  18.     打开视频网页,获取网页原代码并返回
  19.     '''
  20.     html = requests.get(url=url,headers=headers).text
  21.     return html

  22. def findCid(html):
  23.     '''
  24.     从网页源代码中找到cid并返回
  25.     '''
  26.     p = re.compile('cid=[0-9]*')
  27.     cid = p.findall(html)[0].split('=')[1]
  28.     return cid

  29. def openBarrageUrl(cid,headers):
  30.     '''
  31.      打开弹幕网址,提取弹幕

  32.      cid:cid 号
  33.      
  34.      headers: 请求头
  35.     '''
  36.     barrage_url = "https://comment.bilibili.com/{}.xml".format(cid)
  37.     html = requests.get(url=barrage_url,headers=headers)
  38.     xml = html.content.decode('utf-8')
  39.    
  40.     with open(relpath(r"弹幕.xml"),'w',encoding='utf-8') as f:
  41.         f.write(xml)
  42.     p = re.compile('[\u4e00-\u9fa5]+')
  43.     #pprint.pprint(xml)
  44.     #print(p.findall(xml))
  45.     return str(p.findall(xml))
  46.    

  47. def genWordCloud(dm_text):
  48.     '''
  49.     生成词云图

  50.     dm_text:字符串
  51.     '''
  52.     stop_words = {"'"}
  53.     wc = wordcloud.WordCloud(font_path=r"C:\Windows\Fonts\simhei.ttf",width=800,height=600,stopwords=stop_words)
  54.     wc.generate(dm_text)
  55.     image = wc.to_image()
  56.     image.show()

  57. def showtime():
  58.     AV_url = input_lable.get()
  59.     html = openVideoUrl(url=AV_url,headers=headers)
  60.     cid = findCid(html=html)
  61.     dm_data = openBarrageUrl(cid=cid,headers=headers)
  62.     genWordCloud(dm_data)
  63.    
  64. if __name__ == "__main__":
  65.     window = Tk()
  66.     window.title("bilibili视频弹幕词云图生成器")
  67.     window.resizable(0,0)
  68.     input_lable = Entry(window,width = '25',fg = 'white',bg = 'skyblue')
  69.     input_lable.pack()
  70.     input_lable.insert(0,"输入视频网址")
  71.     button = Button(window,text = "生成词云图片",foreground='blue',command = showtime)
  72.     button.pack()
  73.     window.mainloop()
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-2-22 17:16:42 From FishC Mobile | 显示全部楼层    本楼为最佳答案   
出现这种报错的多数是因为相对路径的问题,打包后的程序,相对路径起始点并不在exe所在的文件夹,而是系统临时文件夹内。
试试以下改动打包后是否能运行,我手机写的测试不了(15~19行,51行,如果源码中还有其他涉及相对路径的地方也像51行一样改动):
  1. import os
  2. import requests
  3. import wordcloud
  4. import re
  5. import pprint
  6. from tkinter import *

  7. video_url = "https://www.bilibili.com/video/BV1ep4y1s7xG?t=66"


  8. headers={'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
  9.           'referer':'https://www.bilibili.com',
  10.           'cookie': "_uuid=6185ADAD-E351-4BC4-766E-0B38AF97020786236infoc; buvid3=4990DC29-6DB5-40B2-A10B-6EB9D9AF8F2B155832infoc; sid=d80bw00y; rpdid=|(umRkJu|m~)0J'ulmJl~JuY|; CURRENT_QUALITY=80; LIVE_BUVID=AUTO6915954949231230; blackside_state=1; CURRENT_FNVAL=80; fingerprint=98c99d00d54fe004b372b6bda52ab256; buvid_fp=4990DC29-6DB5-40B2-A10B-6EB9D9AF8F2B155832infoc; buvid_fp_plain=4990DC29-6DB5-40B2-A10B-6EB9D9AF8F2B155832infoc; DedeUserID=431943900; DedeUserID__ckMd5=cd3a9705c2aaafad; SESSDATA=e0703c30%2C1629442221%2Cf30c4*21; bili_jct=64f53090c52cf878f430e6cb30d8c2fb; bsource=search_360; finger=1571944565; bp_video_offset_431943900=494159708514409608; PVID=3"}
  11.          
  12. _rel_origin = os.path.dirname(os.path.abspath(__file__))

  13. def relpath(s):
  14.     """涉及相对路径的地方,套用此函数来获取绝对路径"""
  15.     return os.path.join(_rel_origin, s)




  16. def openVideoUrl(url,headers):
  17.     '''
  18.     打开视频网页,获取网页原代码并返回
  19.     '''
  20.     html = requests.get(url=url,headers=headers).text
  21.     return html

  22. def findCid(html):
  23.     '''
  24.     从网页源代码中找到cid并返回
  25.     '''
  26.     p = re.compile('cid=[0-9]*')
  27.     cid = p.findall(html)[0].split('=')[1]
  28.     return cid

  29. def openBarrageUrl(cid,headers):
  30.     '''
  31.      打开弹幕网址,提取弹幕

  32.      cid:cid 号
  33.      
  34.      headers: 请求头
  35.     '''
  36.     barrage_url = "https://comment.bilibili.com/{}.xml".format(cid)
  37.     html = requests.get(url=barrage_url,headers=headers)
  38.     xml = html.content.decode('utf-8')
  39.    
  40.     with open(relpath(r"弹幕.xml"),'w',encoding='utf-8') as f:
  41.         f.write(xml)
  42.     p = re.compile('[\u4e00-\u9fa5]+')
  43.     #pprint.pprint(xml)
  44.     #print(p.findall(xml))
  45.     return str(p.findall(xml))
  46.    

  47. def genWordCloud(dm_text):
  48.     '''
  49.     生成词云图

  50.     dm_text:字符串
  51.     '''
  52.     stop_words = {"'"}
  53.     wc = wordcloud.WordCloud(font_path=r"C:\Windows\Fonts\simhei.ttf",width=800,height=600,stopwords=stop_words)
  54.     wc.generate(dm_text)
  55.     image = wc.to_image()
  56.     image.show()

  57. def showtime():
  58.     AV_url = input_lable.get()
  59.     html = openVideoUrl(url=AV_url,headers=headers)
  60.     cid = findCid(html=html)
  61.     dm_data = openBarrageUrl(cid=cid,headers=headers)
  62.     genWordCloud(dm_data)
  63.    
  64. if __name__ == "__main__":
  65.     window = Tk()
  66.     window.title("bilibili视频弹幕词云图生成器")
  67.     window.resizable(0,0)
  68.     input_lable = Entry(window,width = '25',fg = 'white',bg = 'skyblue')
  69.     input_lable.pack()
  70.     input_lable.insert(0,"输入视频网址")
  71.     button = Button(window,text = "生成词云图片",foreground='blue',command = showtime)
  72.     button.pack()
  73.     window.mainloop()
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-2-23 14:14:03 | 显示全部楼层
hrp 发表于 2021-2-22 17:16
出现这种报错的多数是因为相对路径的问题,打包后的程序,相对路径起始点并不在exe所在的文件夹,而是系统 ...


的确是路径问题,谢谢
最后还是参考了这篇文章解决的
https://blog.csdn.net/Andone_hsx/article/details/83990023
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-8 15:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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