使用pyqt自动登录5sing.com

#coding=utf-8

__author__ = 'Administrator'

import sys
from PyQt4 import QtCore, QtGui, QtWebKit, QtNetwork

BD_URL = 'http://www.sina.cn'
BD_URL = 'http://5sing.kugou.com/fc/14217069.html'
LOGIN_URL = 'http://5sing.kugou.com/login/'

def setAppProxy():
    proxy = QtNetwork.QNetworkProxy()
    #Http访问代理
    proxy.setType(QtNetwork.QNetworkProxy.HttpProxy)
    #proxy.setType(QtNetwork.QNetworkProxy.DefaultProxy)
    proxy.setHostName(u'proxy3.bj.petrochina')
    proxy.setPort(8080)
    QtNetwork.QNetworkProxy.setApplicationProxy(proxy)

class WebView(QtWebKit.QWebView):
    def __init__(self, parent=None):
        super(WebView, self).__init__(parent)
        self.urlChanged.connect(self.on_url_change)
        self.loadFinished.connect(self.on_load_finish)

    @QtCore.pyqtSlot(QtCore.QUrl, name='urlChange')
    def on_url_change(self, url):
        print(url.toString())

    @QtCore.pyqtSlot(bool, name='loadFinish')
    def on_load_finish(self, ok):
        if self.url() == QtCore.QUrl(LOGIN_URL):
            print(u'加载:%s' % (u'成功' if ok else u'失败'))
            login_script="$('#txtUserName').val('156?????');"\
                         "$('#txtPassword').val('????');"\
                         "$('form').submit();"
            res = self.page().mainFrame().evaluateJavaScript(login_script)
            print(res)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)

    #设置应用程序级的代理
    setAppProxy()

    br = WebView()
    ws = br.settings()

    br.load(QtCore.QUrl(LOGIN_URL))
    br.setWindowTitle(u'浏览器')
    br.show()

    app.exec_()

你可能感兴趣的:(浏览器,PyQt)