问题描述
我正在尝试以字符串形式获取网页数据,而我无法解析它.我没有在 qwebview、qurl 和另一个中找到任何方法.你可以帮帮我吗?Linux、C++、Qt.
i'm trying to get web-page data in string that than i could parse it. I didn't found any methods in qwebview, qurl and another. Could you help me? Linux, C++, Qt.
感谢您的帮助.代码正在运行,但下载后的某些页面已损坏字符集.我尝试过类似的方法来修复它:
Thanks for help. Code is working, but some pages after downloading have broken charset. I tried something like this to repair it:
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?这是一个说明用法的粗略和现成的示例:
Have you looked at QNetworkAccessManager? Here's a rough and ready sample illustrating usage:
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!
}
在您的处理程序中 finished 向您发出信号将传递一个 QNetworkReply 对象,您可以从中读取响应它继承自 QIODevice.一个简单的方法是调用 readAll 来获取QByteArray.您可以从该 QByteArray 构造一个 QString 并执行任何操作你想用它来做.
In your in your handler for the finished signal you will be passed a QNetworkReply object, which you can read the response from as it inherits from QIODevice. A simple way to do this is just call readAll to get a QByteArray. You can construct a QString from that QByteArray and do whatever you want to do with it.
这篇关于我如何获取网页的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!