1

正如标题所暗示的,

我需要从某些需要登录才能使用的网站获取数据。

登录过程可能需要 cookie 或会话。

我需要 QtWebkit,还是只使用 QNetworkAccessManager 就可以了?

我在这两方面都没有经验,我会边走边学。所以请节省我比较两者的时间^^

先感谢您,

埃文

编辑:阅读了一些相关答案后,我将添加一些说明:

相关网站没有 API。所以我需要自己为数据抓取网络元素。我可以只用 QNetworkAccessManager 做到这一点吗?

4

2 回答 2

2

No, in most cases you don't need a full simulated web browser. In most cases, just performing the same web requests like a web browser would do is enough.

Try to record the web requests in your browser, using a plugin like "HTTP Live Headers" or "Firebug" in Firefox. I think Chrome provides a similar tool out of the box. These tools record the GET and POST requests done by the website when you send a form in the webpage.

Another option is to inspect the HTML code of the login page. Find the <form> tag and its fields. Put them together in a GET / POST request in your application to simulate the same form.

Remember that some pages use randomized "tokens" in their forms, some set the tokens as cookies. In such cases, you need to request the login page itself in your application first (before sending the filled in form). Both QWebView and QNetworkAccessManager have cookie support.

To sum things up, I think QWebView provides a far more elegant way to simulate user interaction with a web page. The manual way is, however, more "lightweight", as you don't need Webkit and your application might be faster (because only the HTML page is loaded, without any linked resources like images, CSS, javascript files).

于 2012-07-13T12:14:57.990 回答
0

QWebView as class name states is a view, so it views something (in this case web pages). If you don't need to display loaded page, then you don't need a view. QNetworkAccessManager may do the work, but you need some knowledge about HTTP protocol, and also anything about target site: how does it hande logins, what type of request you have to send to login etc.

于 2012-07-13T12:14:49.680 回答