<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Python的.py文件打包成exe可執行文件

        前幾天做了幾個簡單的爬蟲python程序,于是就想做個窗口看看效果。


        首先是,窗口的話,以前沒怎么接觸過,就先考慮用Qt制作簡單的ui。這里用前面sinanews的爬蟲腳本為例,制作一個獲取當天sina頭條新聞的窗口。

        生成py文件后,運行該py文件,這里窗口我只是隨便拖了幾個組件進去,主要的text browser用于顯示獲取到的sinanews。

        首先貼一下我的配置(點擊即可官方下載): Python 3.3.3

                            PyQt5-5.2.1 for Py3.3(當安裝完Python3.3后,安裝對應PyQt,其會找到Python安裝目錄,不用更改安裝目錄)

        Python3.3默認是沒有安裝pip的,需要下載get-pip.py運行之后,提示安裝成功。

      接下來就要安裝一些必要的組件了。為了安裝方便,先把pip添加進環境變量。下面我們就可以用pip命令安裝組件了。

      先把sina_news.py貼出來,觀察需要哪些組件。

      import requests
      from bs4 import BeautifulSoup
      res = requests.get('http://news.sina.com.cn/china/')
      res.encoding = 'utf-8'
      soup = BeautifulSoup(res.text,'html.parser')
      
      for news in soup.select('.news-item'):
          if len(news.select('h2')) > 0:
              h2 = news.select('h2')[0].text
              a = news.select('a')[0]['href']
              time = news.select('.time')[0].text
              print(time,h2,a)

      發現import requests,import BeautifulSoup 所以先來安裝這些組件

      pip install requests
      
      pip install BeautifulSoup4

      當我們把這段代碼貼進窗口代碼后:

      x.py

      # -*- coding: utf-8 -*-
      
      # Form implementation generated from reading ui file 'x.ui'
      #
      # Created by: PyQt5 UI code generator 5.8.1
      #
      # WARNING! All changes made in this file will be lost!
      import sys
      import requests
      from PyQt5 import QtCore, QtGui, QtWidgets
      from bs4 import BeautifulSoup
      
      class Ui_x(object):
          def getNews():
              res = requests.get('http://news.sina.com.cn/china/')
              res.encoding = 'utf-8'
              soup = BeautifulSoup(res.text,'html.parser')
              title = []
              for news in soup.select('.news-item'):
                  if len(news.select('h2')) > 0:
                      h2 = news.select('h2')[0].text
                      title.append(h2)
                      a = news.select('a')[0]['href']
                      time = news.select('.time')[0].text
              return '\n'.join(title)
      
          
          def setupUi(self, x):
              x.setObjectName("x")
              x.resize(841, 749)
              self.timeEdit = QtWidgets.QTimeEdit(x)
              self.timeEdit.setGeometry(QtCore.QRect(310, 10, 141, 31))
              self.timeEdit.setObjectName("timeEdit")
              self.dateEdit = QtWidgets.QDateEdit(x)
              self.dateEdit.setGeometry(QtCore.QRect(100, 10, 191, 31))
              self.dateEdit.setObjectName("dateEdit")
              self.textBrowser = QtWidgets.QTextBrowser(x)
              self.textBrowser.setGeometry(QtCore.QRect(60, 80, 701, 641))
              self.textBrowser.setObjectName("textBrowser")
              self.retranslateUi(x)
              QtCore.QMetaObject.connectSlotsByName(x)
      
          def retranslateUi(self, x):
              _translate = QtCore.QCoreApplication.translate
              x.setWindowTitle(_translate("x", "x"))
      
      if __name__ == '__main__':   
          app = QtWidgets.QApplication(sys.argv)
          Form = QtWidgets.QWidget()
          ui = Ui_x()
          ui.setupUi(Form)
          Form.show()
          ui.textBrowser.setText(Ui_x.getNews())
          sys.exit(app.exec_())

       

      如果前面順利的話,現在用python運行x.py應該能看到顯示的窗口。

      下面就是打包的過程了,這里筆者用的Pyinstaller,沒有安裝的話,要安裝一下:

      pip install pyinstaller

      安裝完成后,cmd路徑cd到x.py所在目錄。打包命令:

      Pyinstaller -w x.py

      此時,在x.py便生成dist文件夾,打包的x.exe就在此文件夾下。雙擊x.exe顯示效果:

      當然還有許多改進的地方,比如在上面選擇日期,獲得指定日期的頭條新聞。

      筆者在這片博文主要介紹py文件的打包過程。

      可能遇到的問題

      打開打包后的程序無法運行
      顯示:
      ImportError: No module named 'queue'

      During handling of the above exception, another exception occurred:

      Traceback (most recent call last):
      File "test.py", line 2, in <module>
      File "c:\users\hasee\appdata\local\programs\python\python35-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module
      exec(bytecode, module.__dict__)
      File "site-packages\requests\__init__.py", line 63, in <module>
      File "c:\users\hasee\appdata\local\programs\python\python35-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module
      exec(bytecode, module.__dict__)
      File "site-packages\requests\utils.py", line 24, in <module>
      File "c:\users\hasee\appdata\local\programs\python\python35-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module
      exec(bytecode, module.__dict__)
      File "site-packages\requests\_internal_utils.py", line 11, in <module>
      File "c:\users\hasee\appdata\local\programs\python\python35-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module
      exec(bytecode, module.__dict__)
      File "site-packages\requests\compat.py", line 11, in <module>
      File "c:\users\hasee\appdata\local\programs\python\python35-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module
      exec(bytecode, module.__dict__)
      File "site-packages\requests\packages\__init__.py", line 29, in <module>
      ImportError: No module named 'urllib3'
      Failed to execute script test

       

      當然這個錯誤代碼,當時我沒有保留,這是版本不匹配造成的:

      我的Pyinstaller為3.2

      需要降低requests的版本,requests2.10可以成功打包,而2.11就不行。這里貼上解決此問題用到的requests2.10不知道以后會不會修復這個問題。這個bug昨天做夢我還夢到呢。今天早上起來就解決了,興奮的受不了。希望在此過程中遇到的問題對你會有所幫助。

                            

       

       

      posted @ 2017-03-19 14:23  dearvee  閱讀(4484)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 欧美日韩国产图片区一区| 国色天香中文字幕在线视频| 婷婷丁香五月亚洲中文字幕| 中文字幕人妻不卡精品| 欧美成人精品手机在线| 日韩av综合免费在线| 最新国产AV最新国产在钱| 人妻内射一区二区在线视频| 国产精品日韩中文字幕熟女| 中方县| 久热色视频精品在线观看| 亚洲精品码中文在线观看| 成人精品天堂一区二区三区| 欧美乱妇高清无乱码免费| 亚洲精品中文字幕尤物综合| 福利在线视频一区二区| 福利网午夜视频一区二区| 国产对白老熟女正在播放 | 啊灬啊灬啊灬快灬高潮了电影片段 | 久久毛片少妇高潮| 国产av熟女一区二区三区| 东方四虎在线观看av| 男受被做哭激烈娇喘gv视频 | 国产精品国产高清国产av| 加勒比亚洲天堂午夜中文| 国产亚洲精品第一综合另类无码无遮挡又大又爽又黄的视频 | 亚洲综合精品第一页| 熟女少妇精品一区二区| 日韩精品亚洲不卡一区二区| 鲁丝一区鲁丝二区鲁丝三区| 国产一区二区三区精品综合| 日本一区二区三区视频一| 免费人成视频在线播放| 精品亚洲精品日韩精品| 人妻18毛片A级毛片免费看| 亚洲人妻一区二区精品| 精品少妇av蜜臀av| 日本高清一区免费中文视频| 国产av亚洲精品ai换脸电影 | 亚洲男人av天堂久久资源| 国产在线拍揄自揄视频网试看|