爬虫

我们之前将所有的功能写到一个java文件中,这样不足之处在于:当代码量大的时候,该文件就会很臃肿,也不便于进行维护(例如:修改某个功能,可能会查找半天)

所以,我们要将不同的功能写到对应的java文件中,便于后期的维护

搭建项目结构:

  1. JsoupHTML.java,负责通过jsoup解析html文档,获取需求的内容

  2. ExcelHandle.java,负责通过java生成Excel文件,将网页内容保存到Excel文件

  3. phone.java,该文件称为实体类,保存解析的数据((手机品牌 配置 价格 点评数量))

  4. Test.java,测试文件

1.手机实体类

/**
 * Phone实体类 以对象的形式保存工作信息
 * @author XuYang
 *
 */
public class Phone {

	public Phone() {
		
	}
	//品牌  配置  价格  分数 点评人数  排名  
	private String phName;//手机品牌
	private String phConfig;//手机配置
	private String phPrice;//手机价格
	private String phScore;//手机得分
	private String phNumOfPerple;//点评人数
	private String phRank;//手机排名
	public String getPhName() {
		return phName;
	}
	public void setPhName(String phName) {
		this.phName = phName;
	}
	public String getPhConfig() {
		return phConfig;
	}
	public void setPhConfig(String phConfig) {
		this.phConfig = phConfig;
	}
	public String getPhPrice() {
		return phPrice;
	}
	public void setPhPrice(String phPrice) {
		this.phPrice = phPrice;
	}
	public String getPhScore() {
		return phScore;
	}
	public void setPhScore(String phScore) {
		this.phScore = phScore;
	}
	public String getPhNumOfPerple() {
		return phNumOfPerple;
	}
	public void setPhNumOfPerple(String phNumOfPerple) {
		this.phNumOfPerple = phNumOfPerple;
	}
	public String getPhRank() {
		return phRank;
	}
	public void setPhRank(String phRank) {
		this.phRank = phRank;
	}
	public Phone(String phName, String phConfig, String phPrice, String phScore, String phNumOfPerple, String phRank) {
		this.phName = phName;
		this.phConfig = phConfig;
		this.phPrice = phPrice;
		this.phScore = phScore;
		this.phNumOfPerple = phNumOfPerple;
		this.phRank = phRank;
	}
	@Override
	public String toString() {
		return "Phone [phName=" + phName + ", phConfig=" + phConfig + ", phPrice=" + phPrice + ", phScore=" + phScore
				+ ", phNumOfPerple=" + phNumOfPerple + ", phRank=" + phRank + "]";
	}
	
	
	
}

2.解析xml方法

public class JsoupHtml {

	//1.封装方法 解析指定的html文件
	public Document  getDocument(String url) throws IOException{
		return Jsoup.connect(url).get();
	}
	
	//2.解析document文档中的指定内容
	public List getPhoneInfo(Document doc){
		//声明ArrayList集合,保存多个手机信息
		List<Phone>  list=new ArrayList<Phone>();
		
		//解析document文档 将手机信息先保存到集合中
		//通过分析发现 所有工作都在并列的li标签
		Elements elements = doc.getElementsByAttribute("data-follow-id");
		//品牌  配置  价格  分数 点评人数  排名  
		for (int i = 0; i < elements.size(); i++) {
			Element ele=elements.get(i);
			
			Phone ph=new Phone();

			String  phoneName=ele.getElementsByTag("img").attr("alt");
			ph.setPhName(phoneName);
			String phoneCon=ele.getElementsByTag("span").first().text();
			ph.setPhConfig(phoneCon);
			
			String phonePrice= ele.getElementsByClass("price-type").text();
		
			ph.setPhPrice(phonePrice);
			String phoneScore= ele.getElementsByClass("score").text();
			ph.setPhScore(phoneScore);
			String phoneNum= ele.getElementsByClass("comment-num").text();
			ph.setPhNumOfPerple(phoneNum);
			String phoneRank= ele.getElementsByClass("rank-row").text();
			
			ph.setPhRank(phoneRank);
			
			
			list.add(ph);
		}
		return list;
		
	}
}

3.ExcelHandle处理

/**
 * Excel的处理
 * 先获取保存Phone的集合   将其保存到Excel
 * @author XuYang
 *
 */
public class ExcelHandle {

	//将集合中手机信息保存到excel
	public void  writeExcel(List list) throws FileNotFoundException, IOException{
		//1.先创建一个workbook工作薄
		HSSFWorkbook workbook=new HSSFWorkbook();
		
		//2.创建工作表
		HSSFSheet sheet=workbook.createSheet("手机信息");
		
		//3.创建第一行
		HSSFRow  firstRow=sheet.createRow(0);
		//品牌  配置  价格  分数 点评人数  排名
		String [] arr={"品牌","配置","价格","分数","人数","排名"};
		//4.创建第一行的5列
		HSSFCell  cell;
		for (int i = 0; i < arr.length; i++) {
			cell=firstRow.createCell(i);
			cell.setCellValue(arr[i]);
		}
		
		//5.遍历工作信息集合 每遍历一次创建1行5列
		HSSFRow  row;
		HSSFCell cell2;
		for (int i = 0; i < list.size(); i++) {
			//每遍历一次集合 获取某个具体的phone
			Phone ph=(Phone) list.get(i);
			
			row=sheet.createRow(i+1);
			
			cell2=row.createCell(0);
			cell2.setCellValue(ph.getPhName());
			
			cell2=row.createCell(1);
			cell2.setCellValue(ph.getPhConfig());
			
			cell2=row.createCell(2);
			cell2.setCellValue(ph.getPhPrice());
			
			cell2=row.createCell(3);
			cell2.setCellValue(ph.getPhScore());
			
			cell2=row.createCell(4);
			cell2.setCellValue(ph.getPhNumOfPerple());
			
			cell2=row.createCell(5);
			cell2.setCellValue(ph.getPhRank());

			
			
		}
		workbook.write(new FileOutputStream("E://Phone.xls"));
	}
	
}

4.测试

public class Test {
	public static void main(String[] args) throws IOException  {
		//1.根据url地址解析document文档
		JsoupHtml jHt=new JsoupHtml();
		
		String url="http://detail.zol.com.cn/cell_phone_index/subcate57_list_1.html";
		
		Document document=jHt.getDocument(url);
		
		//2解析文档中的html信息
		List list=jHt.getPhoneInfo(document);
		
		//3.将工作岗位的集合保存到excel
		ExcelHandle  handle=new ExcelHandle();
		
		handle.writeExcel(list);

		System.out.println("保存成功");
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值