QtWebkit读取html元素

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

主要用了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已输出到桌面!");
}

源码下载

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值