通过前面的学习,我们已经可以对不需要登录的网页正常访问,但现在的网页大部分都需要用户注册,因此这里以学人人网为例,学习一下网站的模拟登录。
首先对http://www.renren.com/进行爬取,对得到的内容进行分析,可以看到画红线的部分,当点击“登录”时,触发的动作是跳转到这个页面:http://www.renren.com/PLogin.do。
package RenRen;
import org.apache.http.client.methods.CloseableHttpResponse;
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.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
* 获取得到人人网的登录页面:
* http://www.renren.com/PLogin.do
public class RR_Preprocess {
final static Logger log = LoggerFactory.getLogger(RR_Preprocess.class);
public static void main(String[] args) throws IOException {
//创建HttpClient
CloseableHttpClient httpClient = HttpClients.createDefault();
//目标网址
String url = "http://www.renren.com";
//请求方法
HttpGet httpGet = new HttpGet(url);
//发送请求,获得响应
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
//判断响应码
int statusCode = httpResponse.getStatusLine().getStatusCode();
if(statusCode == 200){
//获取网页实例
String entity = EntityUtils.toString(httpResponse.getEntity());
//Jsoup解析网页
Document document = Jsoup.parse(entity);
log.info(document.toString());
因此可以将登录的用户名和密码预先封装好,然后直接以Post方法提交用户名和密码访问http://www.renren.com/PLogin.do页面,然后对于登录成功的用户跳转到该请求返回的一个新网址中:
然后以直接以Get方法请求该页面即可:
package RenRen;
import org.apache.http.Header;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
* 由RR_Preprocess.java得到登录跳转页面
* 这里先以Post方法请求登录页面,然后再以Get方法请求登录后的页面
public class RR_Login {
public static void main(String[] args){
//创建HttpClient
CloseableHttpClient httpClient = HttpClients.createDefault();
//请求的目标网址
String rr_url = "http://www.renren.com/PLogin.do";
HttpPost httpPost = new HttpPost(rr_url);
//以Post方式请求,设置登录用户名和密码
List<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("email", "******")); //自己用户名
nameValuePairs.add(new BasicNameValuePair("password", "******"));//自己密码
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
System.out.println(httpResponse);
//获取请求头
Header header = httpResponse.getFirstHeader("Location");
if(header != null){
//以Get方法请求得到重定向的URL
HttpGet httpGet = new HttpGet(header.getValue());
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String res = httpClient.execute(httpGet, responseHandler);
System.out.println(res);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
参考资料:
1、http://blog.csdn.net/qy20115549/article/details/52249232
Git代码
—-首先为什么能用程序去登陆网站?因为当我们访问网站后,服务端会生成一个sessionId,保存在客户端的cookie中,如何这个sessionId是正确登陆是返回的id,那么当我们访问需要登录才能访问的网页时,将这个sessionId加入访问数据中,服务器就知道我们已经登录了,所以就可以爬取需要登录的页面的内容我们需要用到的工具:
Jsoup:http://download.csdn.net/d
http://192.168.1.119/blog11/login/login3.shtml?username=xxx&password=xxx
(2)利用okhttp3模拟登录并抓取数据。
需要引入的jar包,okhttp-4.9.1.jar,kotlin-stdlib-1.5.0-M2.jar,okio-2.10.0.jar
使用jsoup工具可以解析某个URL地址、HTML文本内容,是java爬虫很好的优势,也是我们在网络爬虫不可缺少的工具。本文小编带领大家使用jsoup 实现java爬虫模拟登陆,通过省力的API,很好的实现java爬虫模拟登陆。一、使用工具:Jsoupjsoup 是一款Java 的HTML解析器,可直接解析某个URL地址、HTML文本内容。它提供了一套非常省力的API,可通过DOM,CSS以及类似...
我是怀着无比激动的心情写下的这篇文章,搞了我一周多终于算是成功的模拟了登录。我是在看这位博主的一篇博客以后,对他的代码做了部分改动,才弄好的大家赶紧去看啊,晚了就没了。
我先说怎么改的,省的有些人性子急,看不下去。
文中提到的博主的项目没有使用maven的形式,而是采用了jar包,我down下来他的代码,运行以后,控制台报错,一个是SSL的错,一个就是说jsoup.parse方法,解析了空数据。我...
构造带有认证的Http请求
当请求需要用户名与密码的认证时,需要构造一个httpclient包下的UsernamePasswordCredentials对象,通过这样一个验证对象来提醒这是一个带有验证的请求。在实际操作的过程中,往往需要一并设置请求头user-agent,不然也可能会返回401状态码,以下是代码示例。...
介绍刚学到了一种超实用的java爬虫技术htmlunit,先记录一下。htmlunit其实就是一个没有界面的浏览器,操作很简单,就像自己在使用浏览器。本文介绍其简单的几个操作,仅初学了解htmlunit。第一是模拟登录网站,第二是获取网页html源码。准备下载htmlunit的jar包,点击进入官网下载,下载后,里面有十几个jar包,导入到自己的编译环境里。案例说明:31、35、39行是获取元素的...
Java爬虫中怎么爬取需要登录的网站发布时间:2021-02-02 14:36:43来源:亿速云阅读:80作者:小新这篇文章主要介绍Java爬虫中怎么爬取需要登录的网站,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!在做爬虫时,遇到需要登陆的问题也比较常见,比如写脚本抢票之类的,但凡需要个人信息的都需要登陆,对于这类问题主要有两种解决方式:一种方式是手动设置 cookie ,...
前面两章内容阐述了httpClient怎么模拟Http请求和如何解析页面。
接下去会讲在一些实战中遇到的问题。
现在回到我的开发摸索之路,之前说到我所爬取的网页是某个险企提供给合作公司的一个页面,通过账号登录然后爬取指定的数据。
这里就出现本章要写的主题了。模拟登录。
我首先确认登录验证的请求所需要携带的参数:
可以看到,登录需要验证...