网络爬虫---如何取得网页本的文本信息并保存到txt中

jyf823 2011-04-11 02:27:37
想抓取某网页,然后取得网页中的特定信息,并保存到txt中,求高手指点!
...全文
1718 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
星海。 2011-07-14
  • 打赏
  • 举报
回复
用正则表达式就行了呃!

在后台抓取网页中需要的内容,然后把内容存入txt文件中就OK了
至于具体的实现就要自己研究了
林R 2011-07-13
  • 打赏
  • 举报
回复
xue xi le `````````
turing-complete 2011-07-12
  • 打赏
  • 举报
回复
阁下 , 玩儿过 新浪微博没有 ?
给个例子 你看看 ,怎么提取首页滚动消息的

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import util.ICrawler;
import util.InfoNode;

/**
*
* @author mgd
* @ 新浪公共消息爬取器
*/
public class SinaCrawler implements ICrawler {

//新浪微博 基地址
private final static String BASE_URL="http://weibo.com/";

private HttpURLConnection conn;

private ArrayList<InfoNode> nodeList;

//构造方法
public SinaCrawler() {
this.nodeList=new ArrayList<InfoNode>();
}


@Override
public ArrayList<InfoNode> getInfoList() throws IOException {
this.conn = (HttpURLConnection) new URL(BASE_URL).openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(
this.conn.getInputStream(), "utf-8"));
int start;
int temp;
String strUid = "";
String strUname = "";
String strText = "";
String line = "";
while (true) {
line = reader.readLine();
if (line.endsWith("<div class=\"clearit\"></div>")) {
break;// 辅助停止条件
}
if (line.endsWith("<div class=\"itemt\">")) {
reader.readLine();
reader.readLine();// 跳过两行
line = reader.readLine();
start = line.indexOf("uid=") + 5;
temp = start + 10;
strUid = line.substring(start, temp);// 提取uid
start = line.indexOf('>', temp) + 1;
temp = line.indexOf('<', start);
strUname = line.substring(start, temp);// 提取uname
start = line.indexOf("</a>",temp) + 5;// </a>:长度为5
temp = line.indexOf('<', start);
strText = line.substring(start, temp);// 提取状态信息Text
this.nodeList.add(new InfoNode("新浪微博", strUid, strUname,strText));
if(this.nodeList.size() == 20){
break;//主退出条件
}
}
}
reader.close();
this.conn.disconnect();
return this.nodeList;
}

//测试代码
public static void main(String[] args) {
ICrawler crawler=new SinaCrawler();
try {
for(InfoNode iter : crawler.getInfoList()){
System.out.println(iter);
System.out.println();//调试使用
}
} catch (IOException e) {
e.printStackTrace();
}
}
}


InfoNode 类我就不给了 , 你可以用不到它 ,只需要把信息打印一下 就行了
十年砍柴 2011-07-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jyf823 的回复:]
引用 1 楼 a332934725 的回复:

另存为


求详解!
[/Quote]
求右键
feng063589 2011-07-08
  • 打赏
  • 举报
回复
网络爬虫
huntor 2011-04-12
  • 打赏
  • 举报
回复
使用 jsoup 解析网页内容

P.S. ibm develperwoks 网站有几篇使用 XQuery/XQJ 提取网页内容的文章。
jyf823 2011-04-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 a332934725 的回复:]

另存为
[/Quote]

求详解!
wuzg光 2011-04-11
  • 打赏
  • 举报
回复
另存为

51,396

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧