使用 Qt 5.5.1 从 QWebView 获取网站内容

Get contents of website from QWebView using Qt 5.5.1

我正在使用 Qt 5.5.1 并且我使用 QWebview 制作了一个小型浏览器 我想要什么时候
我打开一个网页并按下一个按钮,它会像我使用 get 一样获取网站的内容QnetworkAccessManager 中的方法而不使用它,因为我想从具有登录页面的网站获取数据,因此 URL 在我登录时不会更改并且没有 post 方法到 PHP 来获取数据。
例如,当我登录 www.login.com 时,登录数据显示在同一个 link
我需要知道我可以解决这个问题,或者我是否可以从 QWebview
中打开的网站获取当前数据 备注
当我登录网站并通过在 firefox 中查看源代码时从中获取数据时,登录数据出现在源代码中
这是我试过的

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow) {
        ui->setupUi(this);
        ui->webView->load(QUrl("https://www.login.com")); // load page that have user name and password field
        QWebPage *page = ui->webView->page(); // get the current page 
        manager = page->networkAccessManager(); // get the access manager from this page
    }

    MainWindow::~MainWindow() {
        delete ui;
    }
    // get button
    void MainWindow::on_pushButton_clicked() {
        reply = manager->get(QNetworkRequest(QUrl("https://www.login.com"))); // make get now after login
        connect(reply, SIGNAL(readyRead()),this,SLOT(readyRead()));
        connect(reply, SIGNAL(finished()),this, SLOT(finish()));
    }

    void MainWindow::readyRead() {
        QString str = QString::fromUtf8(reply->readAll()).trimmed(); // read the data
        ui->plainTextEdit->appendPlainText(str);
    }

但是我没有登录就得到了第一页的数据。我想在登录后获取页面内容,请给我任何关于我应该做什么的提示。
更新
我从 firefox 查看页面源代码获取文本输入名称并使用 QUrlQuery 对其进行 post 结果是没有登录的第一页这部分 HTML 代码我得到了它的名称

<label class="label-off" for="dwfrm_ordersignup_orderNo">Numéro de la commande</label> <input class="textinput required" id="anyname" type="text"  name="UserName"  value=""  maxlength="2147483647" placeholder="* UserName" data-missing-error="Saisis ton numéro de commande. "  data-parse-error="Ce contenu est invalide"  data-range-error="Ce contenu est trop long ou trop court"  required="required" />

并且它对其他字段具有相同的代码。
我在 Qt 中用来制作 post

的代码
manager = new QNetworkAccessManager(this);
QUrlQuery query;
query.addQueryItem("UserName", "AAAA");
query.addQueryItem("Password", "BBB");
reply = manager->post(QNetworkRequest(QUrl(ui->lineEdit->text().trimmed())), query.toString().toUtf8());
connect(reply,&QNetworkReply::downloadProgress,this,&MainWindow::progress);
connect(reply,SIGNAL(readyRead()),this,SLOT(readyRead()));
connect(reply, SIGNAL(finished()), this,SLOT(finish()));

我尝试使用我制作的 PHP 页面的 post 代码,它解决了这里的问题它只有 HTML 页

我将使用不同的方法,而不是模拟一个 POST 请求,我将使用 QWebView 正常加载登录页面并使用 Qt 填写用户名、密码并进行假点击在提交按钮上。

关于如何保存登录后的页面,而不是 HTML 代码,Qt 提供的一个好方法是将 Web 视图呈现为 PDF,缺点是丢失 HTML代码。

只要代码够用就可以webview->page()->mainFrame()->toHtml()

看一个简单的例子,请注意您需要根据您的环境调整代码,分析登录页面等

void MainWindow::start()
{
    connect(webview, &QWebView::loadFinished, this, &MainWindow::LogIn);

    QString html = "<html>"
                   "<body>"
                   "<form action=\"https://httpbin.org/post\" method=\"POST\">"
                   "Username:<br>"
                   "<input type=\"text\" name=\"usernameinput\" value=\"abc\">"
                   "<br>"
                   "Password:<br>"
                   "<input type=\"password\" name=\"passwordinput\" value=\"123\">"
                   "<br><br>"
                   "<button name=\"button1\">Submit</button>"
                   "</form>"
                   "</body>"
                   "</html>";

    webview->setHtml(html); //Load yours https://www.login.com, using setHtml just for example
}

void MainWindow::LogIn(bool ok)
{
    disconnect(webview, &QWebView::loadFinished, this, &MainWindow::LogIn); //Disconnect the SIGNAL

    if (!ok)
        return;

    QWebElement document = webview->page()->mainFrame()->documentElement();

    QWebElement username = document.findFirst("input[name=usernameinput]"); //Find the first input with name=usernameinput

    if (username.isNull())
        return;

    username.setAttribute("value", "def"); //Change the value of the usernameinput input even
                                           //if it already has some value

    QWebElement password = document.findFirst("input[name=passwordinput]"); //Do the same for password

    if (password.isNull())
        return;

    password.setAttribute("value", "123456"); //Do the same for password

    QWebElement button = document.findFirst("button[name=button1]"); //Find the button with name "button1"

    if (button.isNull())
        return;

    connect(webview, &QWebView::loadFinished, this, &MainWindow::finished);

    button.evaluateJavaScript("this.click()"); //Do a fake click on the submit button
}

void MainWindow::finished(bool ok)
{
    disconnect(webview, &QWebView::loadFinished, this, &MainWindow::finished);

    if (!ok)
        return;

    QByteArray data;
    QBuffer buffer(&data);

    if (!buffer.open(QIODevice::WriteOnly))
        return;

    QPdfWriter pdfwriter(&buffer);

    pdfwriter.setResolution(100); //In DPI

    webview->page()->setViewportSize(QSize(pdfwriter.width(), pdfwriter.height()));

    QPainter painter;
    painter.begin(&pdfwriter);
    webview->page()->mainFrame()->render(&painter);
    painter.end();

    buffer.close();

    qDebug() << "PDF Size:" << data.size(); //Now you have a PDF in memory stored on "data"
}