Python语言系统学习17:作品——“网络爬虫”实例演示

原文链接:https://blog.csdn.net/weixin_45606497/article/details/103599484

通过对Python语言的学习与练习,利用所学知识简单做了个小项目,知识水平有限,也希望今后对Python语言感兴趣的,能多多开发项目,多多做个小实例出来。

开发环境:Visual Studio Code

操作系统:Microsoft Window 7

Python版本:3.7

参考教材:Python快速编程入门,黑马程序员,人民邮电出版社,2017年9月第1版

参考网址:http://laoliapi.cn/css/94api.php


一、网络爬虫的例子

1、PyQt设计界面:

2、对象命名:

扫描二维码关注公众号,回复: 8680843 查看本文章

3、源程序

 # 系统包
import sys
import requests
from xpinyin import Pinyin
from PyQt5 import QtCore, QtGui, QtWidgets
# 项目包
from Ui_MainWindow import Ui_MainWindow

class CMainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent = None):
        super(CMainWindow, self).__init__(parent)
        self.setupUi(self)

        # 主窗体设置
        self.show()
    def IDcard(self):
        A=self.txtNumber1.text()
        url = "http://laoliapi.cn/king/King.php?type=idcard&msg=" + A
        res = requests.get(url)
        
        a = res.text
        self.txtResult1.setText(a)
        
    def Postcard(self):
        A=self.txtNumber2.text()
        url = "http://laoliapi.cn/king/King.php?type=zip&msg=" + A
        res = requests.get(url)
        
        a = res.text
        self.txtResult2.setText(a)
    def Weather(self):
        p = Pinyin()
        diqu = self.txtNumber3.text()
        pydiqu = p.get_pinyin(diqu,'')
        url = "http://laoliapi.cn/king/King.php?type=weather&msg=" + pydiqu
        res = requests.get(url)
        a = res.text
        self.txtResult3.setText(a)
    def Phone(self):
        A=self.txtNumber4.text()
        url = "http://laoliapi.cn/king/King.php?type=phone&msg=" + A
        res = requests.get(url)
        
        a = res.text
        self.txtResult4.setText(a)
    def Poem(self):
        A=self.txtTitle.text()
        url = "http://laoliapi.cn/king/King.php?type=cts&msg=" + A
        res = requests.get(url)
        
        a = res.text
        self.txtResult6.setText(a)
    def Cycle(self):
        A=self.txtNumber5.text()
        url = "http://laoliapi.cn/king/King.php?type=jieqi&msg=" + A
        res = requests.get(url)
        
        a = res.text
        self.txtResult5.setText(a)
if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = CMainWindow()
    sys.exit(app.exec_())
    

二、注解

1、在这给程序里用到的几个关键词是:

Requests:网络请求库

url:网址

get:到

2、在程序里用到了几个语句:

1、下载安装包

# 系统包
import sys
import requests #下载requests的安装包,从而调用
from xpinyin import Pinyin  #下载xpinyin的安装包,然后调用pinyin,从而实现将文字转化为拼音
from PyQt5 import QtCore, QtGui, QtWidgets

2、定义

 def IDcard(self):
        A=self.txtNumber1.text() #A是txtNumber里面的内容,对应爬虫内容接口的参数
        url = "http://laoliapi.cn/king/King.php?type=idcard&msg=" + A #输入网址
        res = requests.get(url)  #res获取网络上的爬虫的数据
          a = res.text  #a是定义获取数据的格式
        self.txtResult1.setText(a)  #把a的内容输出在txtResult里

3、转化

 def Weather(self):
        p = Pinyin() #用p代替pinyin
        diqu = self.txtNumber3.text() #diqu获取txtNumber3的汉字
        pydiqu = p.get_pinyin(diqu,'')  #用pydiqu把汉字转成拼音
        url = "http://laoliapi.cn/king/King.php?type=weather&msg=" + pydiqu
        res = requests.get(url)
        a = res.text
        self.txtResult3.setText(a)
发布了413 篇原创文章 · 获赞 1104 · 访问量 81万+

猜你喜欢

转载自blog.csdn.net/qingwufeiyang12346/article/details/103777756