前幾天作了幾個簡單的爬蟲python程序,因而就想作個窗口看看效果。html
首先是,窗口的話,之前沒怎麼接觸過,就先考慮用Qt製做簡單的ui。這裏用前面sinanews的爬蟲腳本爲例,製做一個獲取當天sina頭條新聞的窗口。python
生成py文件後,運行該py文件,這裏窗口我只是隨便拖了幾個組件進去,主要的text browser用於顯示獲取到的sinanews。git
首先貼一下個人配置(點擊便可官方下載): Python 3.3.3github
PyQt5-5.2.1 for Py3.3(當安裝完Python3.3後,安裝對應PyQt,其會找到Python安裝目錄,不用更改安裝目錄)bootstrap
Python3.3默認是沒有安裝pip的,須要下載get-pip.py運行以後,提示安裝成功。app
接下來就要安裝一些必要的組件了。爲了安裝方便,先把pip添加進環境變量。下面咱們就能夠用pip命令安裝組件了。ui
先把sina_news.py貼出來,觀察須要哪些組件。this
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 因此先來安裝這些組件url
pip install requests
pip install BeautifulSoup4
當咱們把這段代碼貼進窗口代碼後:spa
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