1、安装模块和配置工具,参考《python例子:翻译器(简单》;
2、运行工具QtDesigner,利用QtDesigner工具箱设计出界面效果(所需要的控件可查看右边区域),保存效果为文件wi.ui;
3、对文件wi.ui执行pyUIC(ui转化为py代码),执行完生成文件wi.py。
1、新建文件weather.py,该文件为项目主文件,初始化页面并显示;
"""爬虫系列-获取城市天气情况"""from PyQt5.QtWidgets import *# 引入自定义模块import wi# 引入库import sysfrom pypinyin import pinyin, Style# 导入urllib库的urlopen函数from urllib.request import urlopen, Request# 导入BeautifulSoupfrom bs4 import BeautifulSoup as bfimport re
class parentWindow(QWidget, wi.Ui_Form):# 初始化方法def __init__(self):# 找到父类 首页面super(parentWindow, self).__init__()# 初始化页面方法self.setupUi(self)# 公共网址self.url = 'https://www.tianqi.com/'# 点击获取天气情况self.getButton.clicked.connect(self.get_data)
if __name__ == '__main__':# 每一个PyQt5应用都必须创建一个应用对象app = QApplication(sys.argv)# 初始化页面window = parentWindow()# 显示首页window.show()sys.exit(app.exec_())
# 文字转拼音def conversion(self, text):# 使用不带声调的拼音pinyin_text = pinyin(text, style=Style.NORMAL)# 将列表中的拼音合并为字符串no_tone_pinyin = ''.join(''.join(toneless) for toneless in pinyin_text)return no_tone_pinyin
def get_weather(self, url, class_name, assembly):try:headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0'}req = Request(url, headers=headers)# 获取的html内容是字节,将其转化为字符串html = urlopen(req)html_text = bytes.decode(html.read())# 获取页面全部信息obj = bf(html_text, 'html.parser')# 获取天气部分的信息elements = obj.find(class_=class_name).text.strip()# 使用正则表达式替换多个连续空白行single_elements = re.sub(r'\n\s*\n', '\n\n', elements)# 天气信息回显到界面assembly.setText(single_elements)except Exception as e:print(f'错误信息:{e}')QMessageBox.information(self, '错误信息', '输入的城市名称有错误,请检查一下')
def get_data(self):city = self.cityEdit.text()if not city:QMessageBox.information(self, '提示信息', '请先输入城市名称')returncode = self.conversion(city)self.get_weather(f'{self.url}{code}', 'weather_info', self.situationEdit)self.get_weather(f'{self.url}{code}/7/', 'weaul', self.situationEdit_2)
"""爬虫系列-获取城市天气情况"""from PyQt5.QtWidgets import *# 引入自定义模块import wi# 引入库import sysfrom pypinyin import pinyin, Style# 导入urllib库的urlopen函数from urllib.request import urlopen, Request# 导入BeautifulSoupfrom bs4 import BeautifulSoup as bfimport reclass parentWindow(QWidget, wi.Ui_Form):# 初始化方法def __init__(self):# 找到父类 首页面super(parentWindow, self).__init__()# 初始化页面方法self.setupUi(self)# 公共网址self.url = 'https://www.tianqi.com/'# 点击获取天气情况self.getButton.clicked.connect(self.get_data)# 获取天气函数def get_data(self):city = self.cityEdit.text()if not city:QMessageBox.information(self, '提示信息', '请先输入城市名称')returncode = self.conversion(city)self.get_weather(f'{self.url}{code}', 'weather_info', self.situationEdit)self.get_weather(f'{self.url}{code}/7/', 'weaul', self.situationEdit_2)# 文字转拼音def conversion(self, text):# 使用不带声调的拼音pinyin_text = pinyin(text, style=Style.NORMAL)# 将列表中的拼音合并为字符串no_tone_pinyin = ''.join(''.join(toneless) for toneless in pinyin_text)return no_tone_pinyin"""# 获取城市天气数据url:请求的网址class_name:获取数据的class名assembly:回显的组件名"""def get_weather(self, url, class_name, assembly):try:headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0'}req = Request(url, headers=headers)# 获取的html内容是字节,将其转化为字符串html = urlopen(req)html_text = bytes.decode(html.read())# 获取页面全部信息obj = bf(html_text, 'html.parser')# 获取天气部分的信息elements = obj.find(class_=class_name).text.strip()# 使用正则表达式替换多个连续空白行single_elements = re.sub(r'\n\s*\n', '\n\n', elements)# 天气信息回显到界面assembly.setText(single_elements)except Exception as e:print(f'错误信息:{e}')QMessageBox.information(self, '错误信息', '输入的城市名称有错误,请检查一下')if __name__ == '__main__':# 每一个PyQt5应用都必须创建一个应用对象app = QApplication(sys.argv)# 初始化页面window = parentWindow()# 显示首页window.show()sys.exit(app.exec_())
原创文章,作者:guozi,如若转载,请注明出处:https://www.sudun.com/ask/78980.html