我html 页面元素:

/html/body/table[2]/tbody/tr[1]/td/table/tbody/tr[1]/td[2]/font
/html/body/table[2]/tbody/tr[1]/td/table/tbody/tr[2]/td[2]/div/font/span
/html/body/table[2]/tbody/tr[3]/td/font/b
/html/body/table[2]/tbody/tr[5]/td/div/table/tbody/tr[1]/td[1]/div/b/font/span

以下是代码实现:

  

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements; public class JsoupParseHtml { public static String getHtmlByUrl(String url){
String html = null;
//创建httpClient对象
HttpClient httpClient = new DefaultHttpClient();
//以get方式请求该URL
HttpGet httpget = new HttpGet(url);
try {
//得到responce对象
HttpResponse responce = httpClient.execute(httpget);
//返回码
int resStatu = responce.getStatusLine().getStatusCode();
//200正常 其他就不对
if (resStatu==HttpStatus.SC_OK) {
//获得相应实体
HttpEntity entity = responce.getEntity();
if (entity!=null) {
//获得html源代码
html = EntityUtils.toString(entity); }
}
} catch (Exception e) {
System.out.println("访问【"+url+"】出现异常!");
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
return html;
} static String txtpathstr="d:\\one\\"; public static void main(String[] args) throws Exception { String contents="";
String urlbase="http://localhost:8080/1.htm"; //String urlbase="http://www.qiushibaike.com/8hr/page/8?s=4513032";//1?s=4513032
contents+=gettxtlist(urlbase); //写入文件
writefile(contents); } public static String gettxtlist(String txturl) throws Exception{ String content="";
Document doc=jsoupconnect(txturl,360000);
//Elements els= doc.select("div.content"); Elements els= doc.select("html"); for(Element el:els){
if (el.select("body").size()>1){
continue;
}
content+=el.text()+"\r\n";
System.out.println();
System.out.println(content);
}
return content;
} public static Document jsoupconnect (String url,int timeout){
Document doc=null;
int retry=5;
while (null==doc&&retry>0){
retry--;
try{
doc= Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.1; rv:5.0)").timeout(timeout).get();
}catch(Exception e){
e.printStackTrace(); }
}
return doc;
} public static void writefile(String txtstr)throws Exception{
File txtpath=new File(txtpathstr);
if (!txtpath.exists()){
txtpath.mkdirs();
}
File htxt=new File(txtpathstr+"test.txt");
BufferedOutputStream outBuff = new BufferedOutputStream(new FileOutputStream(htxt));
outBuff.write(txtstr.getBytes());
outBuff.flush();
outBuff.close();
} }

  

存在问题:只能一次性读取出来,不能按照要求,按照table分开,下一版本会解决这个问题

jsoup 解析html 页面数据的更多相关文章

  1. Android开发探秘之三:利用jsoup解析HTML页面

    这节主要是讲解jsoup解析HTML页面.由于在android开发过程中,不可避免的涉及到web页面的抓取,解析,展示等等,所以,在这里我主要展示下利用jsoup jar包来抓取cnbeta.com网 ...

  2. Jsoup获取部分页面数据失败 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml.

    用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不符合要求. 请求代码如下: private static ...

  3. 使用jsoup解析html页面内容案例

    public String getFaGuiKuTitles(String type, int page) { String href = "http://info.qd-n-tax.gov ...

  4. Jsoup获取部分页面数据失败 Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml

    用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不符合要求. 请求代码如下: private static ...

  5. Android中利用jsoup解析html页面

    学习jsoup :jsoup学习网站 Android 中使用: 添加依赖 implementation 'org.jsoup:jsoup:1.10.1' 直接上代码: package com.load ...

  6. java Jsoup 抓取页面数据

    List<ImageBean> imgList = new ArrayList<ImageBean>(); ImageBean image = null; String ima ...

  7. 使用Jsoup 抓取页面的数据

    需要使用的是jsoup-1.7.3.jar包   如果需要看文档我下载请借一步到官网:http://jsoup.org/ 这里贴一下我用到的 Java工程的测试代码 package com.javen ...

  8. python爬虫解析页面数据的三种方式

    re模块 re.S表示匹配单行 re.M表示匹配多行 使用re模块提取图片url,下载所有糗事百科中的图片 普通版 import requests import re import os if not ...

  9. json-lib-2.4-jdk15.jar所需全部JAR包.rar java jsoup解析开彩网api接口json数据实例

    json-lib-2.4-jdk15.jar所需全部JAR包.rar  java jsoup解析开彩网api接口json数据实例 json-lib-2.4-jdk15.jar所需全部JAR包.rar  ...

随机推荐

  1. canvas模糊事件处理

    不知道大家项目中有没有用到canvas时还有时候会出现模糊的情况: 具体推测可能是屏幕改变了,然而canvas的渲染对象并没有跟着一起变: 这里简单介绍个对象,window.devicePixelRa ...

  2. 防止输入时键盘覆盖掉textfiled

    添加监听者 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardwasChange:) ...

  3. 在windows中使用VMWare安装Mac OS 10.7

    请参考http://www.cnblogs.com/huwlnew/archive/2011/11/15/2250342.html http://unmi.cc/vmware9-install-mac ...

  4. 垃圾回收器 Dispose 和 Finalize 的互补作用

    假如我们程序有两个窗口 Form1.Form2; 当我们关闭一个窗口的时候,会发出一个 终止响应,并将该窗口对象送入终止队列,公共语言运行库的垃圾回收器跟踪着这个对象的生存期,此时就会调用此对象的基类 ...

  5. C++标准库之泛型算法

    本文中算法都是指泛型算法. 基本要点: 1)算法使用迭代器进行操作. 2)不依赖容器,但容器希望使用算法,就必须提供接口. 3)通用算法永远不会执行容器操作.操作仅指:更改容器大小的操作.但,容器内部 ...

  6. Yii在nginx下多目录rewrite

    开发过程中,在root下有多个程序,采用一个域名,以目录的形式访问,可以采用如下的方法进行url重写: rewrite ^(\/[^\/]+)(.*) $1/index.php$2 last; 意为取 ...

  7. 配置阿里云作为yum 源

    第一步:下载aliyum 的yum源配置文件. http://mirrors.aliyun.com/repo/ 第二步:把下载到的repo文件复制到/etc/yum.repo.d/目录下. ----- ...

  8. SQL Server 中可以被锁住的 12 种资源

    第1种: DB 整个数据库 第2种: file 数据库文件 第3种: table 第4种: hobt(堆)BTree(B树) 第5种: extent 一个区(8个8KB页面) 第6种: page 数据 ...

  9. 利用VisualVM监测Azure云服务中的Java应用

    在做Java开发的时候,我们需要在上线之前对程序进行压力测试,对程序的性能进行全面的监控,了解JVM的CPU,内存,GC,classes,线程等等信息,或者在程序上线运行的过程当中以便于诊断问题或者对 ...

  10. Nginx的HTTP模块

    1.HTTP的核心模块.这些HTTP模块会在编译Nginx时自动编译进来,除非使用configure命令禁止编译这些模块.(1)alias指令.该指令用于在URL和文件系统路径之间实现映射.它与roo ...