我试图获取字符串中的网页数据,而不是我可以解析它.我没有在qwebview,qurl和另一个中找到任何方法.你可以帮帮我吗?Linux,C++,Qt.
编辑:
感谢帮助.代码正在运行,但下载后的某些页面已经破坏了字符集.我尝试过这样的东西来修复它:
QNetworkRequest *request = new QNetworkRequest(QUrl("http://ru.wiktionary.org/wiki/bovo")); request->setRawHeader( "User-Agent", "Mozilla/5.0 (X11; U; Linux i686 (x86_64); " "en-US; rv:1.9.0.1) Gecko/2008070206 Firefox/3.0.1" ); request->setRawHeader( "Accept-Charset", "win1251,utf-8;q=0.7,*;q=0.7" ); request->setRawHeader( "charset", "utf-8" ); request->setRawHeader( "Connection", "keep-alive" ); manager->get(*request);
任何结果=(.
你看过QNetworkAccessManager了吗?这是一个粗略的,准备好的样本,用于说明用法:
class MyClass : public QObject { Q_OBJECT public: MyClass(); void fetch(); public slots: void replyFinished(QNetworkReply*); private: QNetworkAccessManager* m_manager; }; MyClass::MyClass() { m_manager = new QNetworkAccessManager(this); connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*))); } void MyClass::fetch() { m_manager->get(QNetworkRequest(QUrl("http://stackoverflow.com"))); } void MyClass::replyFinished(QNetworkReply* pReply) { QByteArray data=pReply->readAll(); QString str(data); //process str any way you like! }
在完成信号的处理程序中,您将传递一个QNetworkReply对象,您可以从QIODevice继承它来读取响应.一个简单的方法是调用readAll来获取QByteArray.你可以从QByteArray 构造一个QString,并做任何你想做的事情.