2017-02-13 54 views
2

我使用Qt 5.5.1並使用QWebview製作小型瀏覽器我想在
時打開一個網頁並按下一個按鈕,它將獲取網站就像我用GET方法QnetworkAccessManager不使用它,因爲我想從網站上有一個登錄頁面的數據,所以當我登錄並沒有POST方法PHP獲取數據的URL不會改變。
例如當我登錄到www.login.com登錄顯示在同一鏈接上的數據
我需要任何想法我可以解決這個問題,或者如果我可以從QWebview
打開網站獲取當前數據 注意
當我登錄到網站,並獲取數據從它通過壓上查看源代碼在Firefox登錄數據出現在源代碼
這是我試過
從QWebView使用Qt 5.5.1獲取網站的內容

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); 
    } 

但我得到第一個pag的數據沒有登錄。我想在登錄後獲取頁面內容,請告訴我應該做什麼。
更新
我認爲從Firefox在網頁源代碼中獲取文本輸入名稱,並使用QUrlQuery由結果作出後,以它的第一頁沒有登錄本的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的使用,使後

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頁面我做和在這裏工作的問題,這只是HTML頁面

+1

你可能要發個帖子方法到服務器,你需要分析的登錄頁面,看到的文本輸入的名稱和它當作一鍵'QUrlQuery',並通過這些相應的值(通常是名和密碼)。有關如何使用Qt完成post方法的示例已發佈[此處](http://stackoverflow.com/a/7445752/2014561)。 –

+0

我更新什麼我都試過,請看一看這個 –

+1

請參閱我的回答這個問題。 –

回答

1

我會用不同的方法,來替代模擬POST請求,我將加載登錄頁面通常與QWebView和使用Qt來填充用戶名,密碼以及做提交按鈕假的點擊。

關於如何保存後登錄頁面怎麼你所看到的,而不是HTML代碼,Qt提供的一個好方法是渲染網頁視圖爲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" 
} 
+0

非常感謝您的回覆,你的努力,我很欣賞這一點,我試試這個代碼,但它由包含寫在它的領域的用戶名和密碼,沒有點擊按鈕按下的登錄頁面的PDF文件!我檢查了源代碼,我發現提交按鈕的類型不是,但

+0

現在有沒有辦法讓代碼中的按鈕點擊?也許是因爲提交的類型不是而是

+0

是的,它取決於對象,它是否具有type = submit,或者是一個按鈕,一個超鏈接等。您需要在頁面上找到該對象的唯一標識符並將其傳遞給'findFirst'。 我編輯了我的答案來顯示一個例子,其中按鈕的名稱= button1。 –