我需要爬行一个网站https://hl.com
的帖子使用坚果,但这个网站要求在某些页面登录。比如个人资料和某些帖子。所以我需要首先认证,我尝试了下面的代码,但它不工作,我得到一个空白的html。
String url="https://hl.com/user/Joanne74";
Connection.Response res =
Jsoup.connect("https://hl.com/login")
.data("email", "email", "password",
"mypassword").method(Method.POST).timeout(0).execute();
Map<String, String> cookies = res.cookies();
Connection connection = Jsoup.connect(url);
org.jsoup.nodes.Document doc = connection.cookies(cookies).timeout(0).get();
发布于 2018-06-16 06:56:39
这个页面很棘手。它在很大程度上依赖于javascript来使用ajax加载动态内容。
https://healthunlocked.com/api/session
(而不是https://healthunlocked.com/login
)。您可以使用浏览器的调试器进行预览。.ignoreContentType(true)
来避免Exception in thread "main" org.jsoup.UnsupportedMimeTypeException: Unhandled content type.
,因为它将JSON作为javascript发送https://healthunlocked.com/user/Joanne74
是无用的,因为它只加载一些javascript,但是您可以使用调试器观察对内容的其他请求:https://healthunlocked.com/api/posts?userId=909195
或https://healthunlocked.com/api/activity?filter=user-activity-public&pageNumber=1&id=909195
或https://healthunlocked.com/api/profile?username=Joanne74&showPrivateFields=false
以获取所需的所有信息,但话又说回来-它是JSON,因此您将需要额外的库而不是jsoup来进一步解析它。<代码>G215
https://stackoverflow.com/questions/50870400
复制相似问题