本文为原创博客,仅供学习使用。未经本人允许禁止复制下来,上传到百度文库等平台。

目录

  • 分析要获取的数据
  • 程序的结构
  • 构建封装数据的model
  • 模拟登陆程序并解析数据
  • 结果展示

分析要获取的数据

下面继续实战,写一个模拟登陆获取汽车之家,用户信息的程序。如果大家对模拟登陆获取数据不太了解,建议看完http://blog.csdn.net/qy20115549/article/details/52249232,我写的这篇含有抓包获取人人网数据的案例程序,研究透之后,再来看这个要轻松很多。

首先,大家打开汽车之家这个网站(http://i.autohome.com.cn/7741675/info)看看,能否直接观测到返回的用户信息数据。如下图所示,你会发现,他直接跳转到了登陆界面,并没有返回我们想看到的用户信息数据。

为更好的讲解,我注册了一个用户,你会在我的程序中,看到用户名及密码。以下是我们要获取的用户信息。即用户的id,用户名。用户出生日期、用户所在地。为此,我们需要建立封装数据对象的Model。有不明白为什么要建Model的,请看我的这篇博客http://blog.csdn.net/qy20115549/article/details/52203722【基于Java的网络爬虫框架

程序的结构

为了简单,本文就没有建框架。程序的目录结果,如上图所示。

构建封装数据的model

以下是我要建的封装数据对象的model,包含要获取的用户ID,用户名,年龄,性别,地域,网站来源,爬该数据的时间点

package renren.renren;
/*   *  合肥工业大学 管理学院 qianyang 1563178220@qq.com*/
public class UserInfo {private String authorId;private String authorname;private String age;private String gender;private String area;private String source;private String craw_time;public String getAuthorId() {return authorId;}public void setAuthorId(String authorId) {this.authorId = authorId;}public String getAuthorname() {return authorname;}public void setAuthorname(String authorname) {this.authorname = authorname;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public String getArea() {return area;}public void setArea(String area) {this.area = area;}public String getSource() {return source;}public void setSource(String source) {this.source = source;}public String getCraw_time() {return craw_time;}public void setCraw_time(String craw_time) {this.craw_time = craw_time;}
}

模拟登陆程序,并解析数据

为了简单起见,下面,我把模拟登陆获取html文件,解析html文件以及main方法,都放到了一个程序中去了。建议大家还是按照我之前写博客中的框架来写,逻辑比较清晰。下面是我的程序:

package renren.renren;import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/*   *  合肥工业大学 管理学院 qianyang 1563178220@qq.com*/
public class Autohome1 {private static String AutohomeRenLoginURL = "http://account.autohome.com.cn/Login/ValidIndex";  HttpResponse response;  private DefaultHttpClient httpclient = new DefaultHttpClient();  /*   *  这里是模拟登陆的过程*/private boolean login() { /** 用户名及密码* 在我们抓包的过程中,我们发现我们的密码,被MD5加密了,所以这里有一个将密码,转换成MD5的形式*/String userName = "";String password = new MD5().GetMD5Code("");  HttpPost httpost = new HttpPost(AutohomeRenLoginURL);  /** 建立一个NameValuePair数组,用于存储欲传送的参数* 不明白这里参数的,请看我前面写的一篇关于模拟登陆网络抓包的的博客*/List<NameValuePair> nvps = new ArrayList<NameValuePair>();  nvps.add(new BasicNameValuePair("domain", "autohome.com.cn"));  nvps.add(new BasicNameValuePair("isauto", "true"));  nvps.add(new BasicNameValuePair("method", "post"));  nvps.add(new BasicNameValuePair("name", userName));  nvps.add(new BasicNameValuePair("pwd", password));  try {  httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));  response = httpclient.execute(httpost);  } catch (Exception e) {  e.printStackTrace();  return false;  } finally {  httpost.abort();  }  return true;  }  private String getText(String redirectLocation) {  HttpPost httpget = new HttpPost(redirectLocation);  ResponseHandler<String> responseHandler = new BasicResponseHandler();  String responseBody = "";  try {  //获取html文件responseBody = httpclient.execute(httpget, responseHandler);} catch (Exception e) {  e.printStackTrace();  responseBody = null;  } finally {  httpget.abort();  }  return responseBody;  }  public String printText(String redirectLocation) {String htmlText=null;if (login()) {  if (redirectLocation != null) {htmlText=getText(redirectLocation);}  } return htmlText;}  /*   *  以下是jsoup解析的过程,有不明白Jsoup的上网搜索其API*  */public List<UserInfo> parser(String CrawlerUrl) { List<UserInfo> UserInfomation = new ArrayList<UserInfo>();Autohome1 autohome = new Autohome1();  String htmltext=autohome.printText(CrawlerUrl);Document doc = Jsoup.parse(htmltext);String authorId="autohome"+doc.select("li[class=current]").select("a").attr("href").replaceAll("\\D","");Elements dataElements=doc.select("div[class=uData]").select("p");String source="autohome";Date date=new Date();DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String craw_time=format.format(date);String area="";String username="";String gender="";String birthday="";for (Element ele:dataElements) {if (ele.text().contains("所在地")) {area=ele.text().replaceAll(Jsoup.parse("&nbsp;").text(), " ").split(":")[1];//System.out.println("area:"+area);}if (ele.text().contains("性别")) {gender=ele.text().replaceAll(Jsoup.parse("&nbsp;").text(), "").split(":")[1];//System.out.println("gender:"+gender);}if (ele.text().contains("生日")) {birthday=ele.text().replaceAll(Jsoup.parse("&nbsp;").text(), "").split(":")[1];//System.out.println("birthday:"+birthday);}if (ele.text().contains("用户名")) {username=ele.text().replaceAll(Jsoup.parse("&nbsp;").text(), "").split(":")[1];//System.out.println("birthday:"+username);}}/*   *  将数据封装在集合中,返回*/UserInfo Info = new UserInfo();Info.setAuthorId(authorId);Info.setAuthorname(username);Info.setAge(birthday);Info.setGender(gender);Info.setArea(area);Info.setSource(source);Info.setCraw_time(craw_time);UserInfomation.add(Info);return UserInfomation;}  public static void main(String[] args){Autohome1 AutoHome = new Autohome1(); /*需要获取网页的地址*/String url="http://i.autohome.com.cn/7741675/info";/*获取数据,解析数据,返回解析之后的数据*/List<UserInfo> authorInfo=AutoHome.parser(url);/*打印数据在控制台,这里可以按照前面博客的方法,写入数据库*/for (UserInfo userinfo:authorInfo) {System.out.println("ID: "+userinfo.getAuthorId()+" Age: "+userinfo.getAge()+" +Area: "+userinfo.getArea());}}
}

上述程序调用了一个MD5加密的程序,下面是MD5的程序。

package renren.renren;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/*   *  合肥工业大学 管理学院 qianyang 1563178220@qq.com*/
public class MD5 {// 全局数组private final static String[] strDigits = { "0", "1", "2", "3", "4", "5","6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };public MD5() {}// 返回形式为数字跟字符串private static String byteToArrayString(byte bByte) {int iRet = bByte;// System.out.println("iRet="+iRet);if (iRet < 0) {iRet += 256;}int iD1 = iRet / 16;int iD2 = iRet % 16;return strDigits[iD1] + strDigits[iD2];}// 返回形式只为数字private static String byteToNum(byte bByte) {int iRet = bByte;System.out.println("iRet1=" + iRet);if (iRet < 0) {iRet += 256;}return String.valueOf(iRet);}// 转换字节数组为16进制字串private static String byteToString(byte[] bByte) {StringBuffer sBuffer = new StringBuffer();for (int i = 0; i < bByte.length; i++) {sBuffer.append(byteToArrayString(bByte[i]));}return sBuffer.toString();}public static String GetMD5Code(String strObj) {String resultString = null;try {resultString = new String(strObj);MessageDigest md = MessageDigest.getInstance("MD5");// md.digest() 该函数返回值为存放哈希值结果的byte数组resultString = byteToString(md.digest(strObj.getBytes()));} catch (NoSuchAlgorithmException ex) {ex.printStackTrace();}return resultString;}
}

结果展示

本文作者:合肥工业大学 管理学院 钱洋
有不明白,请发邮件1563178220@qq.com

网络爬虫模拟登陆获取数据并解析实战(二)相关推荐

  1. 网络爬虫中的模拟登陆获取数据(实例教学1)

    目录 模拟登陆的原因 如何模拟登陆 实战(demo) 模拟登陆的原因 很多网站,我们是无法直接获得服务器返回的数据,需要输入用户名及密码才能看到数据.如我们登陆人人网时,网站网址http://www. ...

  2. 爬虫模拟登陆获取需要的数据

    爬虫的原理就是利用同一样的cookie或者是session去访问需要获取数据的链接,然后解析数据为我所用,现在这种框架有很多,比如WebMagic等开源的框架,本人在开发时候也参考过网上很多方法,有模 ...

  3. C# 利用HttpWebRequest模拟登陆获取数据设置Accept-Encoding为gzip,deflate后返回的网页是乱码处理

    原由:在解决模拟登陆抓取数据的时候post一个地址时老是获取的内容是乱码. 经过检查最终应该是编码是HttpWebRequest.Headers.Add("Accept-Encoding&q ...

  4. 用java实现网络爬虫,实时获取中国地震台网数据

    用java实现网络爬虫,实时获取中国地震台网数据 1.如何从网络中爬取相关数据 2.怎么进行数据处理 3.绘图设计 4. 存在问题: 5.java程序的源文件 5.1 爬虫程序 5.2 绘制柱状图程序 ...

  5. python爬虫设计在哪里_《python 爬虫教程 知乎》 怎样用Python设计一个爬虫模拟登陆知乎...

    <python 爬虫教程 知乎> 怎样用Python设计一个爬虫模拟登陆知乎 python 爬虫教程 知乎2020-09-23 01:45:13人已围观 怎样用Python设计一个爬虫模拟 ...

  6. python实用脚本 知乎_Python 爬虫模拟登陆知乎

    在之前写过一篇使用python爬虫爬取电影天堂资源的文章,重点是如何解析页面和提高爬虫的效率.由于电影天堂上的资源获取权限是所有人都一样的,所以不需要进行登录验证操作,写完那篇文章后又花了些时间研究了 ...

  7. 爬取新浪微博新闻(selenium),包括模拟登陆,数据存储等(适合初学者)

    爬取新浪微博(模拟登陆,数据存储) 写在最开头 下载浏览器驱动 测试驱动是否匹配/font> 模拟登陆 分析待爬取页面信息 保存数据 程序github地址 写在最开头 该程序主要是为爬取新浪微博 ...

  8. 基于python的影评数据分析_基于Python聚焦型网络爬虫的影评获取技术

    龙源期刊网 http://www.qikan.com.cn 基于 Python 聚焦型网络爬虫的影评获取技 术 作者:郭向向 郑嘉慧 苗学芹 来源:<时代金融> 2019 年第 11 期 ...

  9. python 突破b站验证码_Python爬虫模拟登陆哔哩哔哩(bilibili)并突破点选验证码功能...

    写在前面 今天带给大家一个突破点选验证码的案例,利用爬虫模拟登陆哔哩哔哩,并且把一些采坑的地方给大家强调一下! 一.需求分析 模拟登陆哔哩哔哩 网站链接: https://passport.bilib ...

最新文章

  1. 渗透测试---数据库安全: sql注入数据库原理详解
  2. Php传图缩图,使用以下用于上传图像的PHP代码上传时缩小图像大小
  3. GDCM:dicom文件固定方向的测试程序
  4. 大牛书单 | 消息队列方向的好书
  5. Struts 2配置详解
  6. H5调用手机摄像头拍照,如何压缩后上传
  7. 使用Red Hat Enterprise Linux的实时内核
  8. 声明方法java实际开发中泛型使用需要注意的一些问题
  9. 只使用Feign不引入Eureka
  10. java学习(四)static静态变量 和this
  11. CS224N刷题——Assignment3.1_A window into NER
  12. 家长如何使自己的孩子轻松快乐地学好数学和英语?
  13. JMeter的取样器
  14. 与繁重的工作一起修行
  15. 铺地毯(NOIP真题)
  16. 基于Android的健康宝体检app介绍(一)
  17. a++与++a同a--与--a代码拆分
  18. 图像的形状因子计算方法
  19. 【评测】SF9/SF21昆虫细胞培养基
  20. 海思AI芯片(Hi35XX): 图像jpg转.bgr

热门文章

  1. [导入]ASP常用函数:doAlert()
  2. 磁盘阵列——RAID0制作方法
  3. Node.js之十大Web框架
  4. 容器编排技术 -- Init 容器
  5. MySQL--My.cnf配置文件模板 MYSQL AND MARIADB CONFIGURATION FILE TEMPLATE (MY.CNF/MY.INI)
  6. Git教程--如何安装Git 如何高效地使用Git 合理使用Git分支
  7. git 子命令 git tag 常用命令实操教程
  8. Docker Compose 简介、安装、初步体验
  9. 【git clone 报错】fatal: unable to access ‘https://github.com/zimeng303/React.git/‘: Failed to connect
  10. 十分钟搞定 pandas