Java jsoup发布和cookie

Java jsoup发布和cookie,java,screen-scraping,jsoup,Java,Screen Scraping,Jsoup,我试图使用jsoup登录到一个站点,然后刮取信息,我遇到了一个问题,我可以成功登录并从index.php创建一个文档,但我无法获取站点上的其他页面。我知道我需要在发布后设置cookie,然后在尝试打开网站上的另一个页面时加载cookie。但是我该怎么做呢?下面的代码允许我登录并获取index.php Document doc=Jsoup.connect(“http://www.example.com/login.php") .data(“用户名”、“我的用户名”, “密码”、“我的密码”) .p

我试图使用jsoup登录到一个站点,然后刮取信息,我遇到了一个问题,我可以成功登录并从index.php创建一个文档,但我无法获取站点上的其他页面。我知道我需要在发布后设置cookie,然后在尝试打开网站上的另一个页面时加载cookie。但是我该怎么做呢?下面的代码允许我登录并获取index.php

Document doc=Jsoup.connect(“http://www.example.com/login.php")
.data(“用户名”、“我的用户名”,
“密码”、“我的密码”)
.post();

我知道我可以使用ApacheHttpClient来实现这一点,但我不想这样做

当您登录到该站点时,可能正在设置一个授权会话cookie,该cookie需要在后续请求中发送以维护会话

您可以像这样获得cookie:

Connection.Response res = Jsoup.connect("http://www.example.com/login.php")
    .data("username", "myUsername", "password", "myPassword")
    .method(Method.POST)
    .execute();

Document doc = res.parse();
String sessionId = res.cookie("SESSIONID"); // you will need to check what the right cookie name is
然后在下一个请求时发送,如:

Document doc2 = Jsoup.connect("http://www.example.com/otherPage")
    .cookie("SESSIONID", sessionId)
    .get();
//这将为您提供响应。
响应res=Jsoup
.connect(“登录页面URL”)
.数据(“loginField”login@login.com“,”passField“,”pass1234“)
.method(method.POST)
.execute();
//这会给你饼干
Map loginCookies=res.cookies();
//这是我发现的保持会话状态的最简单方法
Document doc=Jsoup.connect(“urlYouNeedToBeLoggedInToAccess”)
.cookies(loginCookies)
.get();
代码所在的位置:

Document doc = Jsoup.connect("urlYouNeedToBeLoggedInToAccess").cookies().get(); 
我一直有困难,直到我把它改成:

Document doc = Jsoup.connect("urlYouNeedToBeLoggedInToAccess").cookies(cookies).get();

现在它可以完美地工作了。

以下是您可以尝试的

import org.jsoup.Connection;


Connection.Response res = null;
    try {
        res = Jsoup
                .connect("http://www.example.com/login.php")
                .data("username", "your login id", "password", "your password")
                .method(Connection.Method.POST)
                .execute();
    } catch (IOException e) {
        e.printStackTrace();
    }
现在保存您的所有cookie并向您想要的其他页面发出请求

//Store Cookies
cookies = res.cookies();
向另一页发出请求

try {
    Document doc = Jsoup.connect("your-second-page-link").cookies(cookies).get();
}
catch(Exception e){
    e.printStackTrace();
}
询问是否需要进一步帮助。

Connection.Response res=Jsoup.connect(“http://www.example.com/login.php")
Connection.Response res = Jsoup.connect("http://www.example.com/login.php")
    .data("username", "myUsername")
    .data("password", "myPassword")
    .method(Connection.Method.POST)
    .execute();
//Connecting to the server with login details
Document doc = res.parse();
//This will give the redirected file
Map<String,String> cooki=res.cookies();
//This gives the cookies stored into cooki
Document docs= Jsoup.connect("http://www.example.com/otherPage")
    .cookies(cooki)
    .get();
//This gives the data of the required website
.数据(“用户名”、“我的用户名”) .数据(“密码”、“我的密码”) .method(Connection.method.POST) .execute(); //使用登录详细信息连接到服务器 Document doc=res.parse(); //这将提供重定向的文件 Map cooki=res.cookies(); //这会将饼干储存在cooki中 文档文档=Jsoup.connect(“http://www.example.com/otherPage") .cookies(cooki) .get(); //这提供了所需网站的数据
@Jonathan Hedley,因为你创建了JSoup,它非常有用。请帮助我,无论我做什么,都在iframe末尾添加<>编码。谢谢你。但是如何获取HttpOnly cookies?该代码对你从网站登录和抓取信息有效吗?因为在我的情况下,它不起作用。你可以在这里看到我的代码,它不起作用现在工作。我正在努力登录并注销一个facebook帐户。现在,facebook引入了更多的参数。lsd:AVptuGRS电子邮件:**pass:**default\u persistent:0时区:-120 lgnrnd:043627\u eQnN lgnjs:1383914188语言环境:en\u US检查此链接:嘿,伙计,我照你说的做了。但是我没有得到“URLyouneedogelogedin访问”的网页。请回答我,不是为我工作<代码>org.jsoup.HttpStatusException:获取URL时出现HTTP错误。Status=400,欢迎使用SO。请阅读后再发布答案。这段代码是什么意思?虽然这段代码可以回答这个问题,但提供关于这段代码为什么和/或如何回答这个问题的附加上下文可以提高其长期价值。
Connection.Response res = Jsoup.connect("http://www.example.com/login.php")
    .data("username", "myUsername")
    .data("password", "myPassword")
    .method(Connection.Method.POST)
    .execute();
//Connecting to the server with login details
Document doc = res.parse();
//This will give the redirected file
Map<String,String> cooki=res.cookies();
//This gives the cookies stored into cooki
Document docs= Jsoup.connect("http://www.example.com/otherPage")
    .cookies(cooki)
    .get();
//This gives the data of the required website