我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

以下是代码实现:

  

  1. import java.io.BufferedOutputStream;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4.  
  5. import org.apache.http.HttpEntity;
  6. import org.apache.http.HttpResponse;
  7. import org.apache.http.HttpStatus;
  8. import org.apache.http.client.HttpClient;
  9. import org.apache.http.client.methods.HttpGet;
  10. import org.apache.http.impl.client.DefaultHttpClient;
  11. import org.apache.http.util.EntityUtils;
  12. import org.jsoup.Jsoup;
  13. import org.jsoup.nodes.Document;
  14. import org.jsoup.nodes.Element;
  15. import org.jsoup.select.Elements;
  16.  
  17. public class JsoupParseHtml {
  18.  
  19. public static String getHtmlByUrl(String url){
  20. String html = null;
  21. //创建httpClient对象
  22. HttpClient httpClient = new DefaultHttpClient();
  23. //以get方式请求该URL
  24. HttpGet httpget = new HttpGet(url);
  25. try {
  26. //得到responce对象
  27. HttpResponse responce = httpClient.execute(httpget);
  28. //返回码
  29. int resStatu = responce.getStatusLine().getStatusCode();
  30. //200正常 其他就不对
  31. if (resStatu==HttpStatus.SC_OK) {
  32. //获得相应实体
  33. HttpEntity entity = responce.getEntity();
  34. if (entity!=null) {
  35. //获得html源代码
  36. html = EntityUtils.toString(entity);
  37.  
  38. }
  39. }
  40. } catch (Exception e) {
  41. System.out.println("访问【"+url+"】出现异常!");
  42. e.printStackTrace();
  43. } finally {
  44. httpClient.getConnectionManager().shutdown();
  45. }
  46. return html;
  47. }
  48.  
  49. static String txtpathstr="d:\\one\\";
  50.  
  51. public static void main(String[] args) throws Exception {
  52.  
  53. String contents="";
  54. String urlbase="http://localhost:8080/1.htm";
  55.  
  56. //String urlbase="http://www.qiushibaike.com/8hr/page/8?s=4513032";//1?s=4513032
  57. contents+=gettxtlist(urlbase);
  58.  
  59. //写入文件
  60. writefile(contents);
  61.  
  62. }
  63.  
  64. public static String gettxtlist(String txturl) throws Exception{
  65.  
  66. String content="";
  67. Document doc=jsoupconnect(txturl,360000);
  68. //Elements els= doc.select("div.content");
  69.  
  70. Elements els= doc.select("html");
  71.  
  72. for(Element el:els){
  73. if (el.select("body").size()>1){
  74. continue;
  75. }
  76. content+=el.text()+"\r\n";
  77. System.out.println();
  78. System.out.println(content);
  79. }
  80. return content;
  81. }
  82.  
  83. public static Document jsoupconnect (String url,int timeout){
  84. Document doc=null;
  85. int retry=5;
  86. while (null==doc&&retry>0){
  87. retry--;
  88. try{
  89. doc= Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.1; rv:5.0)").timeout(timeout).get();
  90. }catch(Exception e){
  91. e.printStackTrace();
  92.  
  93. }
  94. }
  95. return doc;
  96. }
  97.  
  98. public static void writefile(String txtstr)throws Exception{
  99. File txtpath=new File(txtpathstr);
  100. if (!txtpath.exists()){
  101. txtpath.mkdirs();
  102. }
  103. File htxt=new File(txtpathstr+"test.txt");
  104. BufferedOutputStream outBuff = new BufferedOutputStream(new FileOutputStream(htxt));
  105. outBuff.write(txtstr.getBytes());
  106. outBuff.flush();
  107. outBuff.close();
  108. }
  109.  
  110. }

  

存在问题:只能一次性读取出来,不能按照要求,按照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. Eclipse导入jre方法

    处理步骤:引入本机安装的jre1.8的步骤如下:

  2. 正确合理的建立MYSQL数据库索引

    写在前面:索引对查询的速度有着至关重要的影响,理解索引也是进行数据库调优的起点.考虑如下情况,假设数据库中一个表有10^6条记录,DBMS的页面大小为4K,并存储100条记录.如果没有索引,查询将对整 ...

  3. $ cd `dirname $0` 和PWD%/* shell变量的一些特殊用法

    在命令行状态下单纯执行 $ cd `dirname $0` 是毫无意义的.因为他返回当前路径的".". $0:当前Shell程序的文件名dirname $0,获取当前Shell程序 ...

  4. 理解ThreadLocal(一)

    早在JDK 1.2的版本中就提供java.lang.ThreadLocal,ThreadLocal为解决多线程程序的并发问题提供了一种新的思路.使用这个工具类可以很简洁地编写出优美的多线程程序. Th ...

  5. 安装Ubuntu时的硬盘分区方案 转载

    安装Ubuntu时的硬盘分区方案 http://www.cnblogs.com/shenliang123/p/3196743.html 如果你准备在硬盘里只安装Ubuntu一个操作系统的话,建议你采用 ...

  6. Big Number

    问题陈述: 杭州电子科技大学 HANGZHOU DIANZI UNIVERSITY Online Judge Problem - 1018 问题解析: 公式一: n! = 10^m => lg( ...

  7. MYSQL delete 从多人表中删除

    语法 1. delete table_list_A from table_list_B where bool_expression; 从delete_table_list_A 删除 bool expr ...

  8. 清理下NFC的基本概念

    移动支付这事情热了总归还是会回归理性,就如同之前的10几年间的几次轮回一样.字面上看,移动支付比支付大也不大可能,有相同,有扩展,有交集有不通才是. NFC这事情也是说了快十年了,真心希望它能回归到其 ...

  9. openstack 使用cloud init 和 console-log, nbd或者libguestfs 获取VM中的硬件信息。

    以获取PCI的信息为例. 基本代码: pci.py import base64 import guestfs from functools import partial import os impor ...

  10. 【LeetCode练习题】Copy List with Random Pointer

    Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...