五一福利 —— Python 代码之 tbr 浏览器

杨承翰 463 1

上一篇博客已经讲过了井字棋游戏的开发。传送门:

五一福利 —— Python 代码之井字棋

2年前 (2020-05-01) 0
五一福利 —— Python 代码之井字棋

这次我们来开发一个叫做“tbr”的浏览器。[aru_12]

所需 Python 版本

Python 2.7.x

Python 安装方法

Windows

下载以下安装包进行安装即可:

蓝奏云

Linux

请到 官网 获取帮助。[aru_11]

所需 Python 模块

  • PyQt4
  • time(自带)
  • urllib2(通常自带)
  • sys(自带)

模块安装方法

方法一

如果你用的是 Windows,建议使用上文方法安装 Python 2.7。[aru_14]

方法二

请下载这个种子文件中的三个文件夹,并将这三个文件夹移动到 Python 根目录的 Lib\site-packages\ 下。[aru_17] 戳我下载

代码

# -*- coding: UTF-8 -*-

import sys, urllib2, time
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
from PyQt4.QtNetwork import *

# 变量
now_url = 'guide'
dangerous = ['www.github.com', 'github.com'] # 危险网站,这里把 github 当作例子

# 加载网页
class WebView(QWebView):
    def __init__(self, parent = None):
        super(WebView, self).__init__(parent)

    def linkClicked(self, url):
        global now_url
        now_url = str(url).strip("')")
        now_url = now_url.split("PyQt4.QtCore.QUrl(u'")[1]
        browser.addressBar.setText(now_url)
        address = browser.addressBar.text()
        self.load(url)
        if now_url.split('/')[2] in dangerous or now_url in dangerous:
            browser.setWindowTitle(u"YchBrPro - 危险浏览")
            QMessageBox.critical(browser, u"危险!", u"您将要访问危险网站!", QMessageBox.Ok)

# 网页显示部分主体
class WebPage(QWebPage):
    def __init__(self, parent = None):
        super(WebPage, self).__init__(parent)

    def javaScriptConsoleMessage(self, s, l, n):
        QMessageBox.information(browser, u"Console 第%s行显示" % l, str(s), QMessageBox.Ok)
        
    def mousePressEvent(self, e):
        address = str(browser._view.url()).strip("')")
        address = address.split("PyQt4.QtCore.QUrl(u'")[1]
        browser.addressBar.setText(address)

    def userAgentForUrl(self, url):
        return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36 tbr/1.0"

# 浏览器外表框架
class MyBrowser(QWidget):

    def __init__(self, parent = None):
        super(MyBrowser, self).__init__(parent)
        self.showMaximized()
        self.createLayout()
        self.createConnection()

    # 浏览网页
    def search(self):
        address = str(self.addressBar.text())
        if address:
            if address.find('://') == -1 and address != 'guide':
                address = 'http://' + address
            url = QUrl(address)
            self._view.load(url)
            self.addressBar.setText(address)
        address = str(self.addressBar.text())
        if "https://" in address:
            self.setWindowTitle(u"YchBrPro - 安全浏览")
        elif "http://" in address:
            self.setWindowTitle(u"YchBrPro - 未知浏览")
        address = str(self.addressBar.text())
        if address.split('/')[0] + address.split('/')[1] + address.split('/')[2] in dangerous or address in dangerous:
            self.setWindowTitle(u"YchBrPro - 危险浏览")
            QMessageBox.critical(self, u"危险!", u"您将要访问危险网站!", QMessageBox.Ok);
        
    # 各种按钮
    def createLayout(self):
        self.setWindowTitle(u"YchBrPro - 安全浏览")
        self.setWindowIcon(QIcon('favicon.png'))
        self.addressBar = QLineEdit()
        self.addressBar.setText("https://ych-template.com/guide.html")
        self.goButton = QPushButton(u"浏览")
        self.renewButton = QPushButton(u"刷新")
        self.backButton = QPushButton(u"后退")
        self.nextButton = QPushButton(u"前进")
        
        self.goButton.setFont(QFont("Microsoft YaHei", 10))
        self.renewButton.setFont(QFont("Microsoft YaHei", 10))
        self.addressBar.setFont(QFont("Consolas", 12))
        self.backButton.setFont(QFont("Microsoft YaHei", 10))
        self.nextButton.setFont(QFont("Microsoft YaHei", 10))
        
        bl = QHBoxLayout()
        bl.addWidget(self.addressBar)
        bl.addWidget(self.goButton)
        bl.addWidget(self.renewButton)
        bl.addWidget(self.backButton)
        bl.addWidget(self.nextButton)

        self.goButton.setCursor(Qt.PointingHandCursor)
        self.renewButton.setCursor(Qt.PointingHandCursor)
        self.backButton.setCursor(Qt.PointingHandCursor)
        self.nextButton.setCursor(Qt.PointingHandCursor)
        
        self._page = WebPage()
        self._page.userAgentForUrl = "Mozila/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36 tbr/1.0"
        self._view = WebView()
        self._view.setPage(self._page)
        self._window = QMainWindow()
        self._window.setCentralWidget(self._view)
        url = QUrl("https://www.ych-template.com/guide.html")
        self._view.load(url)
        layout = QVBoxLayout()
        layout.addLayout(bl)
        layout.addWidget(self._window)
        self._view.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
        self._view.page().linkClicked.connect(self._view.linkClicked)
        self.setLayout(layout)
  
    # 刷新
    def renew(self):
        self._view.reload()
        if now_url.split('/')[2] in dangerous or now_url in dangerous:
            self.setWindowTitle(u"YchBrPro - 危险浏览")
            QMessageBox.critical(browser, u"危险!", u"您将要访问危险网站!", QMessageBox.Ok)

    # 后退
    def goBack(self):
        self._view.back()
        if now_url.split('/')[2] in dangerous or now_url in dangerous:
            self.setWindowTitle(u"YchBrPro - 危险浏览")
            QMessageBox.critical(browser, u"危险!", u"您将要访问危险网站!", QMessageBox.Ok)

    # 前进
    def goNext(self):
        self._view.forward()
        if now_url.split('/')[2] in dangerous or now_url in dangerous:
            self.setWindowTitle(u"YchBrPro - 危险浏览")
            QMessageBox.critical(browser, u"危险!", u"您将要访问危险网站!", QMessageBox.Ok)

    # 对按钮做出反应
    def createConnection(self):
        self.connect(self.addressBar, SIGNAL('returnPressed()'), self.search)
        self.connect(self.addressBar, SIGNAL('returnPressed()'), self.addressBar, SLOT('selectAll()'))
        self.connect(self.goButton, SIGNAL('clicked()'), self.search)
        self.connect(self.renewButton, SIGNAL('clicked()'), self.renew)
        self.connect(self.backButton, SIGNAL('clicked()'), self.goBack)
        self.connect(self.nextButton, SIGNAL('clicked()'), self.goNext)
        self.connect(self.goButton, SIGNAL('clicked()'), self.addressBar, SLOT('selectAll()'))


app = QApplication(sys.argv)

browser = MyBrowser()
browser.show()

sys.exit(app.exec_())

依旧是没有很多注释 …… 应该不需要吧 …… [aru_31]

这里还有一个图片需要下载,就是本站的图标:

请注意,只有 Chrome / 火狐 可以下载!其它浏览器用户请切换浏览器或点击下方按钮并手动保存图片。[aru_33]

 戳我下载

请将此图片放置至 py 文件的同一文件夹。

还是上次的那句话,Python 2 与 Python 3 不兼容,请卸载其中一个![aru_42]

使用代码、运行、打包的方法都在上一篇博客讲过。传送门在博客的第一段。[aru_22]

发表评论 取消回复
表情 图片 链接 代码

  1. 凉秋影
    凉秋影 Lv 1

    不错的哈

分享