最近跟舍友@小疯一起研究爬虫
他写了个小应用-优快云博客爬虫 有兴趣的朋友可以点进去看看哦~
一起学习。
一起进步。
想要源代码的朋友点击这里下载哦~
听到“爬虫”,是不是第一时间想到python/php ? 多少想玩爬虫的java学习者就因为语言不通而止步。Java是真的不能做爬虫吗?
当然不是。
只不过python的3行代码能解决的问题,而Java要30行。
这里推荐大家一个大牛做的java爬虫框架 【WebMagic】
文档简单易懂!java爬虫开发的福利啊!
一起来动手做一个小应用吧!
爬虫小应用–知乎用户信息
爬虫思想有3步
1. 抽取目标链接
2. 抽取需要的信息
3. 处理数据
一、 抽取目标链接 (确定入口地址,这里的入口是https://www.zhihu.com/search?type=people&q=java)
接下来查看html结构,确定待爬取的目标链接。(这里我的目标链接是【前10个用户的详细信息页面的url】)
二、抽取需要的信息(webmagic提供了3种方式,xpath,css选择,正则表达式。具体可以查看下[WebMagic文档](http://webmagic.io/docs/zh/))
确定好【目标的信息】,如下图。
创建对应的实体对象
package entity;
/**
* 知乎用户信息
* @author antgan
*
*/
public class ZhihuUser {
private String key;//keyword
private String name;//用户名
private String identity;//身份
private String location;//所在地
private String profession;//行业
private int sex;//性别
private String school;//学校
private String major;//专业
private String recommend;//个人简介
private String picUrl;//头像url
private int agree;//赞同
private int thanks;//感谢
private int ask;//提问数
private int answer;//回答数
private int article;//文章数
private int collection;//收藏数
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIdentity() {
return identity;
}
public void setIdentity(String identity) {
this.identity = identity;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getProfession() {
return profession;
}
public void setProfession(String profession) {
this.profession = profession;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getRecommend() {
return recommend;
}
public void setRecommend(String recommend) {
this.recommend = recommend;
}
public String getPicUrl() {
return picUrl;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
public int getAgree() {
return agree;
}
public void setAgree(int agree) {
this.agree = agree;
}
public