环境&开发库 beautifulsoup4+python3.3+win7+eclipse-pydev
截图如下:
'''
天气预报
@author: Administrator
'''
from bs4 import BeautifulSoup
from bs4.element import Tag
import urllib
from urllib import request
class WeatherBean(object):
'''
天气bean
'''
def __init__(self, value):
self.value = value
#北京天气预报
city=""
#天气预报类型,白天或者黑夜
type=""
#晴间多云
phrase=""
#最高:7℃
temperature=""
#北风3-4级
wind=""
def setAttr(self,attr,value):
'''
设置属性
'''
self.attr=value
def getWeather(self):
'''
获取天气
'''
return self.city+","+self.type+","+self.phrase+","+self.temperature+","+self.wind
def printWeather(self):
'''
打印
'''
print(self.city)
print(self.type)
print(self.phrase)
print(self.temperature)
print(self.wind)
print('<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<')
def getHtmlByClass(html,class_name):
'''
根据class属性,获取文档dom
'''
if type(html)==Tag:
return html.find('', {"class":class_name})
soup = BeautifulSoup(html)
return soup.find('', {"class":class_name})
def getWeatherByType(sup):
'''
根据class属性,获取文档dom
'''
wer_bean=WeatherBean(wind_class)
wer_bean.city=getHtmlByClass(html,city_class).h1.string
wer_bean.type=sup.dt.string
wer_bean.phrase=getHtmlByClass(sup,phrase_class).string
temperature=getHtmlByClass(sup,temperature_class).contents
wer_bean.temperature=temperature[0].string+temperature[1].string+temperature[2].string
wer_bean.wind=getHtmlByClass(sup,wind_class).contents[0].string
return wer_bean
def getHtmlById(html,id_name):
'''
根据ID属性,获取文档dom
'''
if type(html)==Tag:
return html.find('', {"id":id_name})
soup = BeautifulSoup(html)
return soup.find('', {"id":id_name})
night_class='night'
day_class='day'
city_class='btitle'
phrase_class='phrase'
wind_class='wind'
temperature_class='temperature'
html=urllib.request.urlopen("http://tianqi.2345.com/today-54511.htm").read();
soup=BeautifulSoup(html)
#获取天的htmlsoup
daySoup=getHtmlByClass(html,day_class)
nightSoup=getHtmlByClass(html,night_class)
print(getWeatherByType(daySoup).getWeather())
print(getWeatherByType(nightSoup).getWeather())
截图如下:
在控制台输出太不方便了,就像用gui搞下,于是用pyqt designer搞下:
怎么把pyQt Designer 生成的 *.ui文件转换为 *.py?
在开始菜单下方的输入框输入pyuic4 -o ui_xxx.py xxx.u
i将 ui_xxx.py 替换为要生成的 **.py文件(包含路径)(一般都保留前面的ui_)xxx.ui 替换为要编译的 **.ui 文件(包含路径)
比如,要把C盘shit文件夹中的 myui.ui 转为**.py 并保存到 D盘下spam文件夹中,
只需在命令行输入:pyuic4 -o D:\spam\ui_myui.py C:\shit\myui.ui按回车即可,注意空格
我的路径是
E:\tools\python3.3.2\Lib\site-packages\PyQt5\pyuic5.bat
C:\Users\Administrator>pyuic5.bat -o d:\main.py d:\main.ui
pyqt天气预报:
main.py:
'''
天气预报
@author: Administrator
'''
from bs4 import BeautifulSoup
from bs4.element import Tag
import urllib
from urllib import request
class WeatherBean(object):
'''
天气bean
'''
def __init__(self, value):
self.value = value
#北京天气预报
city=""
#天气预报类型,白天或者黑夜
type=""
#晴间多云
phrase=""
#最高:7℃
temperature=""
#北风3-4级
wind=""
def getAttr(self,attr):
'''
获取属性
'''
return self.attr;
def setAttr(self,attr,value):
'''
设置属性
'''
self.attr=value
def getWeather(self):
'''
获取天气
'''
return self.city+","+self.type+","+self.phrase+","+self.temperature+","+self.wind
def printWeather(self):
'''
打印
'''
print(self.city)
print(self.type)
print(self.phrase)
print(self.temperature)
print(self.wind)
print('<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<')
def getHtmlByClass(html,class_name):
'''
根据class属性,获取文档dom
'''
if type(html)==Tag:
return html.find('', {"class":class_name})
soup = BeautifulSoup(html)
return soup.find('', {"class":class_name})
def getWeatherByType(sup):
'''
根据class属性,获取文档dom
'''
wer_bean=WeatherBean(wind_class)
wer_bean.city=getHtmlByClass(html,city_class).h1.string
wer_bean.type=sup.dt.string
wer_bean.phrase=getHtmlByClass(sup,phrase_class).string
temperature=getHtmlByClass(sup,temperature_class).contents
wer_bean.temperature=temperature[0].string+temperature[1].string+temperature[2].string
wer_bean.wind=getHtmlByClass(sup,wind_class).contents[0].string
return wer_bean
def getHtmlById(html,id_name):
'''
根据ID属性,获取文档dom
'''
if type(html)==Tag:
return html.find('', {"id":id_name})
soup = BeautifulSoup(html)
return soup.find('', {"id":id_name})
night_class='night'
day_class='day'
city_class='btitle'
phrase_class='phrase'
wind_class='wind'
temperature_class='temperature'
html=urllib.request.urlopen("http://tianqi.2345.com/today-54511.htm").read();
soup=BeautifulSoup(html)
def getNght():
#获取夜间的htmlsoup
nightSoup=getHtmlByClass(html,night_class)
print(getWeatherByType(nightSoup).getWeather())
return getWeatherByType(nightSoup)
def getDay():
#获取天的htmlsoup
daySoup=getHtmlByClass(html,day_class)
print(getWeatherByType(daySoup).getWeather())
return getWeatherByType(daySoup)
if(__name__=="__main__"):
getDay()
min.py(pyqt designer 设计UI文件,UI文件转换为py文件):
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'd:\main.ui'
#
# Created: Mon Dec 9 16:40:59 2013
# by: PyQt5 UI code generator 5.0
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtWidgets
from main import getDay, getNght
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(528, 363)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.city = QtWidgets.QLabel(self.centralwidget)
self.city.setGeometry(QtCore.QRect(40, 70, 91, 16))
self.city.setObjectName("city")
self.type = QtWidgets.QLabel(self.centralwidget)
self.type.setGeometry(QtCore.QRect(40, 100, 71, 31))
self.type.setObjectName("type")
self.temp = QtWidgets.QLabel(self.centralwidget)
self.temp.setGeometry(QtCore.QRect(40, 190, 63, 13))
self.temp.setObjectName("temp")
self.wind = QtWidgets.QLabel(self.centralwidget)
self.wind.setGeometry(QtCore.QRect(40, 220, 63, 13))
self.wind.setObjectName("wind")
self.phear = QtWidgets.QLabel(self.centralwidget)
self.phear.setGeometry(QtCore.QRect(40, 150, 63, 13))
self.phear.setObjectName("phear")
self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
self.calendarWidget.setGeometry(QtCore.QRect(190, 60, 264, 184))
self.calendarWidget.setObjectName("calendarWidget")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
wb=getDay()
MainWindow.setWindowTitle(_translate("MainWindow", getNght().getWeather()))
self.city.setText(_translate("MainWindow", wb.city))
self.type.setText(_translate("MainWindow", wb.type))
self.temp.setText(_translate("MainWindow", wb.temperature))
self.wind.setText(_translate("MainWindow", wb.wind))
self.phear.setText(_translate("MainWindow", wb.phrase))
test.py(运行win窗口):
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from min import Ui_MainWindow
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())