写了个解析 抓取百度搜索结果的类,比较简单,大家找茬,BUG多

本文介绍了一种使用Java实现的爬虫程序,该程序能够从百度搜索引擎中抓取指定关键词的相关网页信息,包括网址、标题及简介等内容。通过解析HTML源码并提取有用的数据,此工具为数据分析和信息整理提供了便利。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题不少,大家看看 提提意见啊!!! :oops:

package test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

public class readBaidu {



public List readHTML(String s) throws Exception {

List list = new ArrayList();

while(s.indexOf("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td class=f>")>0){

baiduBean bb = new baiduBean();

int start = s.indexOf("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");

int end = s.indexOf("</table>", start)+8;

String content = s.substring(start,end);

bb.setContents(this.getShow(content.replace(" ", "")));
bb.setUrl(this.getURL(content.replace(" ", "")));
bb.setTitle(this.getTitle(content.replace(" ", "")));

s = s.replace(content, "");

list.add(bb);
}


return list;
}


public String getContents(String path)throws Exception{

StringBuffer contents = new StringBuffer();


URL url = new URL(path);

URLConnection uc = url.openConnection();

BufferedReader bf = new BufferedReader(new InputStreamReader(uc.getInputStream()));

String nowLine = "";

while((nowLine=bf.readLine())!=null){

contents.append(nowLine);

}

return contents.toString();

}

public String getURL(String con){

String url = "";

int srartURL = con.indexOf("href=\"")+6;

int endURL = con.indexOf("target=", srartURL)-1;

url = con.substring(srartURL,endURL);

return url;

}

public String getTitle(String con){

String title = "";

int startCon = con.indexOf("<fontsize=")+14;

int endCon = con.indexOf("</a>",startCon);

title = con.substring(startCon,endCon);

title = title.replace("fontcolor", "font color");

return title;

}


public String getShow(String con){

String contents = "";

String br = "";

int startBR = con.indexOf("<br>")+17;

int endBR = con.indexOf("<br>",startBR);

br = con.substring(startBR,endBR);

br = br.replace("fontcolor", "font color");

return br;

}



public static void main(String[] args)throws Exception{


String path = "http://www.baidu.com/s?wd=csdn&pn=10";

readBaidu r = new readBaidu();

String rest = r.getContents(path);

List list = r.readHTML(rest);

for (int i = 0; i < list.size(); i++) {

baiduBean b = (baiduBean)list.get(i);

System.out.println(b.url);
System.out.println(b.title);
System.out.println(b.contents);


}

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值