使用QWebEnginePage下载网页。效果很好,但有时不适用于某些URL,似乎是标题问题。我不知道要发送什么标头才能收到此错误。
标题:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QApplication>
#include <QWebEnginePage>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
QWebEnginePage *p;
Ui::MainWindow *ui;
protected slots:
void getHtml(bool s);
void textChanged();
signals:
};
#endif // MAINWINDOW_H
来源
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QWebEnginePage>
#include <QWebEngineSettings>
#include <QWebEngineHttpRequest>
QWebEngineHttpRequest httpR;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow){
ui->setupUi(this);
p = new QWebEnginePage(this);
httpR.setHeader("Location", "absoluteURI");
httpR.setUrl(QUrl("https://ca.finance.yahoo.com/quote/AIPT/history?period1=1238597365&period2=1554130165&interval=1d&filter=history&frequency=1d"));
p->settings()->setAttribute(QWebEngineSettings::AutoLoadImages, false);
p->settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
connect(p, SIGNAL(loadFinished(bool)), this, SLOT(getHtml(bool)));
p->load(httpR);
}
QString html = "";
void MainWindow::getHtml(bool s){
p->toHtml(
[this](QString result) {
html=result;
this->textChanged();
qDebug()<<httpR.headers();
});
}
void MainWindow::textChanged(){
qDebug()<<html.size();
}
MainWindow::~MainWindow(){
delete ui;
}
错误消息:
js:无法识别的Content-Security-Policy指令“ disown-opener”。
js:无法识别的Content-Security-Policy指令“ disown-opener”。
860801 QVector(“位置”) js:无法识别的内容安全策略 指令“ disown-opener”。
js:无法识别的Content-Security-Policy指令“ disown-opener”。
js:无法识别的Content-Security-Policy指令“ disown-opener”。
js:资源 https://pagead2.googlesyndication.com/pagead/js/r20190327/r20190131/show_ads_impl.js已使用链接预加载进行了预加载,但几秒钟内未使用 从窗口的加载事件。请确保它请确保它 具有适当的as值,并有意预加载。
答案 0 :(得分:0)
所以我终于想通了。似乎可以解决“无法识别的Content-Security-Policy指令'disown-opener'” 错误,您必须在每个错误中合并html标签属性“ rel = noopener” 链接标签。但是,由于我想从其他来源下载html网页,因此更改每个链接会有些困难。
经过反复试验,我发现此标头“升级:websocket” 修复了该问题。