java网络爬虫模拟登录案例教学2
本文为原创博客,仅供技术学习使用。未经允许,禁止将其复制下来上传到百度文库等平台。
模拟登录的网站
我们需要登录的网站为:autonews,模拟登陆的地址为:
https://home.autonews.com/clickshare/cspLogin.do
网络抓包分析
在请求表单中,输入用户名及密码。
可以看到,我们要向后台提交的数据有哪些。
CSParamsToPassNames:CSParamsToPassNames
userLogin.userName:你的用户名
userLogin.password:你的密码
CSDropAuthCookieSpecified:1
CSDropAuthCookie:1
submit:Sign In
实战代码
package crawlerTest;
/* * 合肥工业大学 管理学院 qianyang 1563178220@qq.com*/
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;@SuppressWarnings("deprecation")
public class AutonewsLogin {// The configuration items //输入用户名及密码private static String userName = ""; private static String password = ""; private static String redirectURL = "https://home.autonews.com/clickshare/myhome.do"; // Don't change the following URL private static String renRenLoginURL = "https://home.autonews.com/clickshare/cspLogin.do"; // The HttpClient is used in one session private HttpResponse response; private DefaultHttpClient httpclient = new DefaultHttpClient(); private boolean login() { //open the LoginURLHttpPost httpost = new HttpPost(renRenLoginURL); // All the parameters post to the web site//建立一个NameValuePair数组,用于存储欲传送的参数List<NameValuePair> nvps = new ArrayList<NameValuePair>(); // nvps.add(new BasicNameValuePair("CSAuthReq", "1")); // nvps.add(new BasicNameValuePair("CSTargetURL", "http%3A%2F%2Fwww.autonews.com%2F")); // nvps.add(new BasicNameValuePair("CSResumeURL", "/clickshare/forceLogin.do")); // nvps.add(new BasicNameValuePair("CSParamsToPassNames", "CSAuthReq|CSTargetURL|CSResumeURL|CSParamsToPassNames")); nvps.add(new BasicNameValuePair("userLogin.userName", userName)); nvps.add(new BasicNameValuePair("userLogin.password", password)); // nvps.add(new BasicNameValuePair("CSDropAuthCookieSpecified", "1")); // nvps.add(new BasicNameValuePair("CSDropAuthCookie", "1")); nvps.add(new BasicNameValuePair("submit", "Sign In")); try { httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); response = httpclient.execute(httpost); int StatusCode = response.getStatusLine().getStatusCode();System.out.println(StatusCode);} catch (Exception e) { e.printStackTrace(); return false; } finally { httpost.abort(); } return true; } private String getRedirectLocation() { Header locationHeader = response.getFirstHeader("Location"); if (locationHeader == null) { return null; } return locationHeader.getValue(); } private String getText(String redirectLocation) { HttpGet httpget = new HttpGet(redirectLocation); // Create a response handler ResponseHandler<String> responseHandler = new BasicResponseHandler(); String responseBody = ""; try { responseBody = httpclient.execute(httpget, responseHandler); } catch (Exception e) { e.printStackTrace(); responseBody = null; } finally { httpget.abort(); httpclient.getConnectionManager().shutdown(); } return responseBody; } public String printText() { String html="";if (login()) { String redirectLocation = getRedirectLocation(); if (redirectLocation != null) { html=getText(redirectURL); } } return html;} public static void main(String[] args) throws IOException { AutonewsLogin AutonewsLogin = new AutonewsLogin(); BufferedWriter writer = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( new File("D:\\d.txt")),"gbk"));String html=AutonewsLogin.printText(); writer.write(html);writer.close();AutonewsLogin.downloadFile("http://www.autonews.com/assets/PDF/CA11537753.PDF","E:\\zipFile\\","1");} //下载该网页的pdf文件public void downloadFile(String fileURL, String saveDir,String fileName)throws IOException {File fileDir=new File(saveDir);if(!fileDir.exists()){fileDir.mkdirs();}//图片或zip下载保存地址String filename=saveDir+fileName+".pdf";File file=new File(filename);if(file.exists()){file.delete();}BufferedOutputStream bw = new BufferedOutputStream(new FileOutputStream(filename)); HttpGet httpGet=new HttpGet(fileURL);HttpResponse httpResponse = httpclient.execute(httpGet);try {HttpEntity entity=httpResponse.getEntity();int i=-1;byte[] byt= EntityUtils.toByteArray(entity); bw.write(byt);System.out.println("文件下载成功!");} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}bw.close();}
}
程序结果
java网络爬虫模拟登录案例教学2相关推荐
- Java网络爬虫实战案例一
紧接我们上次的问题,如何获取服务器发送的资源,保存到本地?上一篇文章见java网络爬虫核心原理. 一.Java IO流三分游(input,output) 我们知道计算机是用来处理数据的.所有的程序 ...
- Java网络爬虫小案例(详细版)
有bug:修改了<scope>test</scope>后,在控制台还是不能显示日志信息,没找到解决办法 配置了log4j.properties,控制台没有显示日志信息_连胜是我 ...
- 第三十六期:学 Java 网络爬虫,需要哪些基础知识?
说起网络爬虫,大家想起的估计都是 Python ,诚然爬虫已经是 Python 的代名词之一,相比 Java 来说就要逊色不少.有不少人都不知道 Java 可以做网络爬虫,其实 Java 也能做网络爬 ...
- Java网络爬虫该如何学习
文章目录 引言 怎么入门网络爬虫 课程特色 学完本课程能收获什么 引言 互联网以及移动技术的飞速发展,使得全球数据量呈现前所未有的爆炸式增长态势.例如,用户在互联网上的搜索数据.交易数据.评论数据.社 ...
- Java网络爬虫学习记录(请求基础篇)
目录 个人实验遇见错误集: 一.javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX ...
- python网络爬虫、Java 网络爬虫,哪个更好?
说起网络爬虫,大家想起的估计都是 Python ,诚然爬虫已经是 Python 的代名词之一,相比 Java 来说就要逊色不少.有不少人都不知道 Java 可以做网络爬虫,其实 Java 也能做网络爬 ...
- 学 Java 网络爬虫,需要哪些基础知识?
说起网络爬虫,大家想起的估计都是 Python ,诚然爬虫已经是 Python 的代名词之一,相比 Java 来说就要逊色不少.有不少人都不知道 Java 可以做网络爬虫,其实 Java 也能做网络爬 ...
- Java网络爬虫入门:第01课:网络爬虫原理
引言 随着互联网的迅速发展,网络资源越来越丰富,信息需求者如何从网络中抽取信息变得至关重要.目前,有效的获取网络数据资源的重要方式,便是网络爬虫技术.简单的理解,比如您对百度贴吧的一个帖子内容特别感兴 ...
- Java网络爬虫实操(5)
上一篇:Java网络爬虫实操(4) 大家好,前几篇文章介绍的URL都是返回HTML内容的,然后再从HTML字符串里解析出我们想要的数据. 但是,随着前端编程技术的发展,至少十多年前开始ajax.jso ...
- java 网络爬虫 正则表达式_【干货】Java网络爬虫基础知识
原标题:[干货]Java网络爬虫基础知识 引言 Java 网络爬虫具有很好的扩展性可伸缩性,其是目前搜索引擎开发的重要组成部分.例如,著名的网络爬虫工具 Nutch 便是采用 Java 开发,该工具以 ...
最新文章
- Spring框架系列之AOP思想
- 激励理论在人力资源管理中的运用
- Thread类(线程)
- 以太坊开发 发行代币的增发功能 并实现转到钱包 和买卖的功能
- 神策数据通过中国信通院 SDK 安全评测
- css clearfix(针对火狐height:auto无效解决方案)
- linux mysql 实战_linux实用实战
- android 自定义圆形pop,Android布局自定义Shap圆形ImageView可以单独设置背景与图片...
- java打jar包的方式,jar命令,maven
- jQuery中文手册, jQuery API, jQuery UI, 分页插件 下载
- GitHub 迎来内置 CI/CD,对所有开源项目免费!
- php preg_match 只匹配第一个字符_PHP正则表达式核心技术完全详解 第3节
- Python-copy()与deepcopy()之间的主要区别
- 百度编辑器复制微信图片无法保存
- 计算机的存储器与高速缓存
- System Toolkit 3.3.3 中文版 系统维护工具箱
- 室内全景图数据集的设计及制作流程
- Digital Clock 3D for Mac(3D机械时钟动态桌面壁纸)
- FIO测试硬盘IOPS
- 金山办公2020笔试