我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. Opencv--HoughCircles源码剖析

    图形可以用一些参数进行表示,标准霍夫变换的原理就是把图像空间转换成参数空间(即霍夫空间),例如霍夫变换的直线检测就是在距离-角度空间内进行检测.圆可以表示成: (x-a)2+(y-b)2=r2     ...

  2. 一个SQL 建表格式

    CREATE TABLE [dbo].[SysSample]([Id] [varchar](50) NOT NULL,[Name] [varchar](50) NULL,[Age] [int] NUL ...

  3. pl sql练习(3)

    1.s树形结构查询表中的数据:比如emp表中每个员工都有自己的头,即公司中的职位是按层次划分的,类似一个树,因此有时需要按层次显示查询的结果. select empno,mgr,ename,job f ...

  4. 获取mssqlserver与access数据库插入的当前行的id

    //mssqlserver public static int GetInsertId(string sql) { try { SqlCommand cmd = new SqlCommand(); u ...

  5. Linux Power(一): kernel/power/earlysuspend.c

    /* kernel/power/earlysuspend.c * * Copyright (C) 2005-2008 Google, Inc. * * This software is license ...

  6. nginx的conf文件的详细配置

    #定义Nginx运行的用户和用户组user www www; #nginx进程数,建议设置为等于CPU总核心数.worker_processes 8; #全局错误日志定义类型,[ debug | in ...

  7. iOS绘制view

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  8. PHP设计模式之委托模式

    委托模式: 通过分配或委托至其他对象,委托设计模式能够去除核心对象中的判决和复杂的功能性. class Bank{ protected $info; /* 设置基本信息 @param string $ ...

  9. perl 实现微信简版<2>

    <pre name="code" class="python">use LWP::UserAgent; use URI::Escape; use N ...

  10. linux之SQL语句简明教程---UNION ALL

    UNION ALL 这个指令的目的也是要将两个 SQL 语句的结果合并在一起. UNION ALL 和UNION 不同之处在于 UNION ALL 会将每一笔符合条件的资料都列出来,无论资料值有无重复 ...