Jsoup cookie验证从cookiesyncmanager从https网站抓取 [英] Jsoup cookie authentication from cookiesyncmanager to scrape from https site
问题描述
我有一个android应用程序使用webview,用户必须用用户名和密码登录之前,重定向到我想用jsoup来刮除数据的页面。由于jsoup线程将是一个不同的会话,用户将不得不重新登录。
I have an android application using a webview on which the user has to log in with username and password before being redirected to the page i would like to scrape data off with jsoup. Since the jsoup thread would be a different session the user would have to login again.
现在我想使用从webview接收到的cookie与jsoup请求一起发送,以便能够抓取我的数据。
Now i would like to use the cookie received from the webview to send with the jsoup request to be able to scrape my data.
正在使用以下代码将cookie与cookiesyncmanager同步。这基本上是我被困住,因为我不知道如何读出cookie或如何附加到jsoup请求。请帮忙? :)
The cookie is being synced with cookiesyncmanager with following code. This is basically where I am stuck cause i dont know how to read out the cookie nor how to attach it to the jsoup request. Please help? :)
public void onPageFinished(WebView view, String url) {
CookieSyncManager.getInstance().sync();
jsoup scrape我在用户登录后执行此操作:
The jsoup scrape I am doing after the user has logged in with something like this:
doc = Jsoup.connect("https://need.authentication.com").get();
Elements elements = doc.select("span.tabCount");
Element count = elements.first();
Log.d(TAG, "test"+(count));
推荐答案
Android开发者,但也许你可以尝试这样的:
I'm not an android developer but maybe you can try something like this:
final String url = "https://need.authentication.com";
// -- Android Cookie part here --
CookieSyncManager.getInstance().sync();
CookieManager cm = CookieManager.getInstance();
String cookie = cm.getCookie(url); // returns cookie for url
// ...
// -- JSoup part here --
// Jsoup uses cookies as "name/value pairs"
doc = Jsoup.connect("https://need.authentication.com").header("Cookie", cookie).get();
// ...
我希望这有助于正如我之前说的:im没有android开发人员(代码未经测试!)
I hope this helps a bit, but as i said before: im no android developer (and code isn't tested!)
这里有一些文档:
- CookieManager
- CookieSyncManager
- Jsoup Connection
这篇关于Jsoup cookie验证从cookiesyncmanager从https网站抓取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!