国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python中.py文件打包成exe可執(zhí)行文件詳解

2020-02-23 04:28:28
字體:
供稿:網(wǎng)友

前言

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

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

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

首先貼一下我的配置

官方下載:

       Python 3.3.3

  PyQt5-5.2.1 for Py3.3(當(dāng)安裝完P(guān)ython3.3后,安裝對應(yīng)PyQt,其會找到Python安裝目錄,不用更改安裝目錄)

本地下載:

        Python 3.3.3

        PyQt5-5.2.1 for Py3.3(當(dāng)安裝完P(guān)ython3.3后,安裝對應(yīng)PyQt,其會找到Python安裝目錄,不用更改安裝目錄)

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

接下來就要安裝一些必要的組件了。為了安裝方便,先把pip添加進(jìn)環(huán)境變量。

下面我們就可以用pip命令安裝組件了。

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

import requestsfrom bs4 import BeautifulSoupres = 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)

發(fā)現(xiàn)import requests,import BeautifulSoup 所以先來安裝這些組件

pip install requestspip install BeautifulSoup4

當(dāng)我們把這段代碼貼進(jìn)窗口代碼后:

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 sysimport requestsfrom PyQt5 import QtCore, QtGui, QtWidgetsfrom bs4 import BeautifulSoupclass 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_())            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 漯河市| 淳化县| 北流市| 西青区| 安阳县| 江川县| 贺兰县| 界首市| 时尚| 微山县| 陇川县| 大关县| 姚安县| 修水县| 临泉县| 馆陶县| 大名县| 湖南省| 平乡县| 清远市| 建阳市| 屯留县| 纳雍县| 泾阳县| 六枝特区| 镇康县| 镇远县| 喀喇沁旗| 清镇市| 北辰区| 蒲城县| 内江市| 青海省| 尼勒克县| 奉新县| 泽普县| 湘潭县| 台中市| 舟山市| 平凉市| 武宁县|