jsoup 解析html 页面数据
我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 页面数据的更多相关文章
- Android开发探秘之三:利用jsoup解析HTML页面
这节主要是讲解jsoup解析HTML页面.由于在android开发过程中,不可避免的涉及到web页面的抓取,解析,展示等等,所以,在这里我主要展示下利用jsoup jar包来抓取cnbeta.com网 ...
- Jsoup获取部分页面数据失败 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml.
用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不符合要求. 请求代码如下: private static ...
- 使用jsoup解析html页面内容案例
public String getFaGuiKuTitles(String type, int page) { String href = "http://info.qd-n-tax.gov ...
- Jsoup获取部分页面数据失败 Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml
用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不符合要求. 请求代码如下: private static ...
- Android中利用jsoup解析html页面
学习jsoup :jsoup学习网站 Android 中使用: 添加依赖 implementation 'org.jsoup:jsoup:1.10.1' 直接上代码: package com.load ...
- java Jsoup 抓取页面数据
List<ImageBean> imgList = new ArrayList<ImageBean>(); ImageBean image = null; String ima ...
- 使用Jsoup 抓取页面的数据
需要使用的是jsoup-1.7.3.jar包 如果需要看文档我下载请借一步到官网:http://jsoup.org/ 这里贴一下我用到的 Java工程的测试代码 package com.javen ...
- python爬虫解析页面数据的三种方式
re模块 re.S表示匹配单行 re.M表示匹配多行 使用re模块提取图片url,下载所有糗事百科中的图片 普通版 import requests import re import os if not ...
- 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 ...
随机推荐
- MySQL基础之 path环境变量的作用 (科普)
在谈mysql配置环境变量之前,先谈一下windows环境变量的作用,有时候在windows cmd窗口运行命令时,经常会出现“XXX不是内部或外部命令的提示” 原因是系统没有找到执行相应命令的文件( ...
- C++ primer学习记录(个人猜想未测试版本)
学习版本:第五版. 本博文主要记录个人曾经并不知晓知识细节. 因为linux下的编译环境还未进行学习.所以实际代码测试将在今后完成. 红色:需确认. 蓝色:重点. 1)const对象设定为仅在文件内有 ...
- BaseFragment的定义—所有Fragment的父类
public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener { prote ...
- FireDAC
http://docs.embarcadero.com/products/rad_studio/firedac/frames.html Access: http://docwiki.embarcade ...
- Android平台对H264视频硬解码
本文讲述如何使用Android标准的API (MediaCodec)实现H264的硬件解码. 原本我们是用JNI调用平台提供的硬件解码接口得到YUV帧,再放入opengl脚本里处理渲染的.可是换了新平 ...
- Windows 10上快速尝鲜bash on Ubuntu
今年微软Build 2016大会最让开发人员兴奋的消息之一,就是在Windows上可以原生运行Linux bash,对于非开发人员来讲,可能不知道这意味着什么,而对于开发人员来说,意味着Windows ...
- 浏览器兼容问题汇总<转>
浏览器的内核 Mozilla Firefox ( Gecko ) Internet Explorer ( Trident ) Opera ( Presto ) Safari ( WebKit ) Go ...
- js的框架
Ember.js的一些学习总结 1.1.1 摘要 现在,我们经常都可以看到复杂的JavaScript应用程序,由于这些应用程序变得越来越复杂,一长串的jQuery回调语句或者通过应用程序在各个状态 ...
- 几个常用道的macro
几个常用道的macro1.macro(1)#error msg 指令使編譯器停止執行並打印一條語句,(2)printf("%d,%s",_LINE_,_FILE_)打印當前行號和文 ...
- Matlab与.NET混编解决人脸识别问题
原文 http://www.cnblogs.com/asxinyu/archive/2013/05/29/3107013.html 如果这些文章对你有用,有帮助,期待更多开源组件介绍,请不要吝啬手中的 ...