How can i get content of web-page

前端 未结 3 1403
误落风尘
误落风尘 2020-12-06 02:26

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.

相关标签:
3条回答
  • 2020-12-06 02:49

    Have you looked into lynx, curl, or wget? In the past I have needed to grab and parse info from a website, sans db access, and if you are trying to get dynamically formatted data, I believe this would be the quickest way. I'm not a C guy, but I assume there is a way to run shell scripts and grab the data, or at least get the script running and grab the output from a file after writing to it. Worst case scenario, you could run a cron and check for a "finished" line at the end of the written file with C, but I doubt that will be necessary. I suppose it depends on what you're needing it for, but if you just want the output html of a page, something as east as a wget piped to awk or grep can work wonders.

    0 讨论(0)
  • 2020-12-06 02:58

    Paul Dixon's answer is probably the best approach but Jesse's answer does touch something worth mentioning.

    cURL -- or more precisely libcURL is a wonderfully powerful library. No need for executing shell scripts and parsing output, libCURL is available C,C++ and more languages than you can shake an URL at. It might be useful if you are doing some weird operation (like http POST over ssl?) that qt doesnt support.

    0 讨论(0)
  • 2020-12-06 02:59

    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!
    
    }
    

    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.

    0 讨论(0)
提交回复
热议问题