网络爬虫模拟登陆获取数据并解析实战(二)
原文地址:http://blog.csdn.net/qy20115549/article/details/52250022
目录
#分析要获取的数据
下面继续实战,写一个模拟登陆获取汽车之家,用户信息的程序。如果大家对模拟登陆获取数据不太了解,建议看完
加载中...
原文地址:http://blog.csdn.net/qy20115549/article/details/52250022
目录
#分析要获取的数据
下面继续实战,写一个模拟登陆获取汽车之家,用户信息的程序。如果大家对模拟登陆获取数据不太了解,建议看完
欢迎关注 DataLearner 官方微信,获得最新 AI 技术推送

首先,大家打开汽车之家这个网站(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;
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() {
area;
}
{
.area = area;
}
String {
source;
}
{
.source = source;
}
String {
craw_time;
}
{
.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 ;
().GetMD5Code();
(AutohomeRenLoginURL);
List<NameValuePair> nvps = <NameValuePair>();
nvps.add( (, ));
nvps.add( (, ));
nvps.add( (, ));
nvps.add( (, userName));
nvps.add( (, password));
{
httpost.setEntity( (nvps, HTTP.UTF_8));
response = httpclient.execute(httpost);
} (Exception e) {
e.printStackTrace();
;
} {
httpost.abort();
}
;
}
String {
(redirectLocation);
ResponseHandler<String> responseHandler = ();
;
{
responseBody = httpclient.execute(httpget, responseHandler);
} (Exception e) {
e.printStackTrace();
responseBody = ;
} {
httpget.abort();
}
responseBody;
}
String {
String htmlText=;
(login()) {
(redirectLocation != ) {
htmlText=getText(redirectLocation);
}
}
htmlText;
}
List<UserInfo> {
List<UserInfo> UserInfomation = <UserInfo>();
();
String htmltext=autohome.printText(CrawlerUrl);
Jsoup.parse(htmltext);
String authorId=+doc.select().select().attr().replaceAll(,);
Elements dataElements=doc.select().select();
String source=;
Date date= ();
DateFormat format= ();
String craw_time=format.format(date);
String area=;
String username=;
String gender=;
String birthday=;
(Element ele:dataElements) {
(ele.text().contains()) {
area=ele.text().replaceAll(Jsoup.parse().text(), ).split()[];
}
(ele.text().contains()) {
gender=ele.text().replaceAll(Jsoup.parse().text(), ).split()[];
}
(ele.text().contains()) {
birthday=ele.text().replaceAll(Jsoup.parse().text(), ).split()[];
}
(ele.text().contains()) {
username=ele.text().replaceAll(Jsoup.parse().text(), ).split()[];
}
}
();
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);
UserInfomation;
}
{
();
String url=;
List<UserInfo> authorInfo=AutoHome.parser(url);
(UserInfo userinfo:authorInfo) {
System.out.println(+userinfo.getAuthorId()++userinfo.getAge()++userinfo.getArea());
}
}
}
上述程序调用了一个MD5加密的程序,下面是MD5的程序。
package renren.renren;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
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 String {
bByte;
System.out.println( + iRet);
(iRet < ) {
iRet += ;
}
String.valueOf(iRet);
}
String {
();
( ; i < bByte.length; i++) {
sBuffer.append(byteToArrayString(bByte[i]));
}
sBuffer.toString();
}
String {
;
{
resultString = (strObj);
MessageDigest.getInstance();
resultString = byteToString(md.digest(strObj.getBytes()));
} (NoSuchAlgorithmException ex) {
ex.printStackTrace();
}
resultString;
}
}
#结果展示
本文作者:合肥工业大学 管理学院 钱洋 有不明白,请发邮件1563178220@qq.com