QtWebkit读取html元素

来源:互联网 发布:java哈希表 编辑:程序博客网 时间:2024/06/06 07:41

闲着无聊做个小工具抓特定网站一些免费账号(不通用)。

主要用了Qt5.5.1种的webkit,具体看代码 就不说了,代码很简单:

void MainWindow::on_pushButton_clicked(){    ui->pushButton->setEnabled(false);    QString WwwUrl  = "http://www.ishadowsocks.net/";    QWebPage UrlPage;    UrlPage.mainFrame()->load(QUrl(WwwUrl));    QEventLoop loop;    QObject::connect(&UrlPage ,SIGNAL(loadFinished(bool)) ,&loop ,SLOT(quit()));    loop.exec();    QWebFrame *UrlFrame = UrlPage.currentFrame();    QWebElement doc = UrlFrame->documentElement();    QWebElement IdFree = doc.findFirst("#free");    QWebElementCollection ServerGroup = IdFree.findAll(".col-sm-4");    QString Json = "{\n\"configs\" : [\n";    QString ConfigData;    for(int Server= 0; Server< ServerGroup.count();Server++)    {        QWebElement childServer = ServerGroup.at(Server);        QWebElementCollection childGroup = childServer.findAll("h4");        QString tmpdata;        QString ServerName = childGroup.at(0).toPlainText();        QString ServerPort = childGroup.at(1).toPlainText();        QString ServerPasswd = childGroup.at(2).toPlainText();        QString ServerMethod = childGroup.at(3).toPlainText();        QRegExp rx(":");        ServerName = ServerName.mid(rx.indexIn(ServerName)+1);        ServerPort = ServerPort.mid(rx.indexIn(ServerPort)+1);        ServerPasswd = ServerPasswd.mid(rx.indexIn(ServerPasswd)+1);        ServerMethod = ServerMethod.mid(rx.indexIn(ServerMethod)+1);        tmpdata=QString("  {\n\"server\" : \"%1\",\n\"server_port\" : %2,\n\"password\" : \"%3\",\n\"method\" : \"%4\",\n\"remarks\" : \"\"}\n,\n")\                .arg(ServerName)\                .arg(ServerPort)\                .arg(ServerPasswd)\                .arg(ServerMethod);        ConfigData+=tmpdata;    }    ConfigData.remove(ConfigData.length()-2,1);    QString config;    config.sprintf("],\n\"strategy\" : null,\n\"index\" : 0,\n\"global\" : false,\n\"enabled\" : true,\n\"shareOverLan\" : true,\n\"isDefault\" : false,\n\"localPort\" : 1080,\n\"pacUrl\" : null,\n\"useOnlinePac\" : false,\n\"availabilityStatistics\" : false}");    ConfigData+=config;    Json +=ConfigData;    WriteFile(Json);    ui->pushButton->setEnabled(true);}void MainWindow::WriteFile(QString data){//    QString Path = QCoreApplication::applicationDirPath();    QString Path = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);    QString configname = "gui-config.json";    QString FilePath = Path+"/"+configname;    QFile file(FilePath);    bool ok = file.open(QIODevice::WriteOnly | QIODevice::Text);    if(!ok)    {        QMessageBox::information(this,"Error","File OPen faild!");        return;    }    QTextStream out(&file);    out<<data.toUtf8()<<endl;    file.close();    QMessageBox::information(this, "Sucess","gui-config.json已输出到桌面!");}

源码下载

0 0
原创粉丝点击