Python_杂学系列_4.利用BeautifulSoup和PyQt5做小案例
前言
这是鄙人第一次的小的尝试,当然还不够完善,有待参考,同时希望大家提出意见!
提示:以下是本篇文章正文内容,下面案例可供参考
一、介绍功能
利用requests库和beautifulsoup解析,另加上一个PyQt5的GUI,打造了一个可以输入明星名字,就可以检索出来相应的介绍。eg:
二、程序步骤
1.引入库
代码如下(示例):
import sys
import requests
from PyQt5.QtGui import QColor
from PyQt5.QtWidgets import (QWidget, QLabel, QLineEdit,
QTextEdit, QGridLayout, QApplication, QPushButton, QColorDialog)
from bs4 import BeautifulSoup
2.窗体的创造
代码如下(示例):
def initUI(self):
title = QLabel('请输入一个你喜欢的明星:')
author = QLabel('简介:')
btn1 = QPushButton("确定")
btn2 = QPushButton("选择编辑框颜色")
# btn3 = QPushButton("Review")
self.titleEdit = QLineEdit()
authorEdit = QLineEdit()
self.reviewEdit = QTextEdit()
grid = QGridLayout()
grid.setSpacing(10)
grid.addWidget(title, 1, 0)
grid.addWidget(self.titleEdit, 1, 1, 1, 2)
grid.addWidget(btn1, 1, 2)
grid.addWidget(author, 3, 0)
grid.addWidget(self.reviewEdit, 3, 1, 5, 2)
grid.addWidget(btn2, 3, 0, 5, 1)
col = QColor(0, 0, 0)
btn2.clicked.connect(self.showDialog1)
btn1.clicked.connect(self.info_name)
# reviewEdit.setStyleSheet()
self.setLayout(grid)
self.setGeometry(300, 300, 500, 300)
self.setWindowTitle('Review')
self.show()
3.获取网络资源
代码如下(示例):
def info_name(self):
str=self.titleEdit.text()
url = f"https://www.sogou.com/web?query={str}" # 有了反馈f
didc = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.62"
}
resp = requests.get(url, headers=didc)
html = resp.text
operat = BeautifulSoup(html, "lxml")
for tag in operat.select(
'#sogou_vr_30010254_0 > div.special-wrap.title-newblue.border-radius.baike200107 > div:nth-child(4) > div.wiki-wrap > p'):
x = tag.get_text()[:-2]
self.reviewEdit.setText(x)
## 4.全部代码 代码如下(示例):
import sys
import requests
from PyQt5.QtGui import QColor
from PyQt5.QtWidgets import (QWidget, QLabel, QLineEdit,
QTextEdit, QGridLayout, QApplication, QPushButton, QColorDialog)
from bs4 import BeautifulSoup
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
title = QLabel('请输入一个你喜欢的明星:')
author = QLabel('简介:')
btn1 = QPushButton("确定")
btn2 = QPushButton("选择编辑框颜色")
# btn3 = QPushButton("Review")
self.titleEdit = QLineEdit()
authorEdit = QLineEdit()
self.reviewEdit = QTextEdit()
grid = QGridLayout()
grid.setSpacing(10)
grid.addWidget(title, 1, 0)
grid.addWidget(self.titleEdit, 1, 1, 1, 2)
grid.addWidget(btn1, 1, 2)
grid.addWidget(author, 3, 0)
grid.addWidget(self.reviewEdit, 3, 1, 5, 2)
grid.addWidget(btn2, 3, 0, 5, 1)
col = QColor(0, 0, 0)
btn2.clicked.connect(self.showDialog1)
btn1.clicked.connect(self.info_name)
# reviewEdit.setStyleSheet()
self.setLayout(grid)
self.setGeometry(300, 300, 500, 300)
self.setWindowTitle('Review')
self.show()
def showDialog1(self):
col = QColorDialog.getColor()
if col.isValid():
self.reviewEdit.setStyleSheet('QWidget { background-color: %s }'
% col.name())
def info_name(self):
str=self.titleEdit.text()
url = f"https://www.sogou.com/web?query={str}" # 有了反馈f
didc = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.62"
}
resp = requests.get(url, headers=didc)
html = resp.text
operat = BeautifulSoup(html, "lxml")
for tag in operat.select(
'#sogou_vr_30010254_0 > div.special-wrap.title-newblue.border-radius.baike200107 > div:nth-child(4) > div.wiki-wrap > p'):
x = tag.get_text()[:-2]
self.reviewEdit.setText(x)
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
5.打包成EXE文件
代码如下(示例):
这就是提示,叫我放在C:\Users\Cactus\的路径下。
pip install pyinstaller
总结
能够把这几个一起综合的运用,我是感觉,我还是扶得上墙,学习的路永无止境,好好加油!,给爷敲代码去