【发布时间】:2017-05-03 08:34:58
【问题描述】:
我从 Java 编程开始,不想创建一个简单的“后端爬虫”。为此,我需要 Post 的登录功能,这没问题,但我怎样才能保存 cookie 战争,并且在下一次请求时,脚本不需要再次登录?
你能举个例子吗?我在互联网上找不到解决方案。
也许您能解释一下我如何使用第一页的 Cookie 来执行下一个请求? :)
希望得到您的答复。
对不起,我的英语不好。
这是我的第一个登录测试:P
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class Login {
public static String loginAndGetHTML() throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
String html;
HttpPost HttpPost = new HttpPost("http://www.google.com");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("username", "admin"));
nvps.add(new BasicNameValuePair("password", "1234"));
HttpPost.setEntity(new UrlEncodedFormEntity(nvps));
HttpPost.addHeader("Referer", "http://tutorials.amazingcode.de/login/index.php");
HttpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0");
CloseableHttpResponse response = httpclient.execute(HttpPost);
try {
HttpEntity entity = response.getEntity();
html = EntityUtils.toString(entity);
EntityUtils.consume(entity);
} finally {
response.close();
}
if(html.contains("Falsche Nutzerdaten")) {
throw new Exception("Login fehlgeschlagen");
}
return html;
}
public static String parseHTML(String html) throws Exception {
Document doc = Jsoup.parse(html);
String zahl = doc.getElementById("zahl").text();
return zahl;
}
}
【问题讨论】:
标签: java cookies web-crawler