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 ...
随机推荐
- Eclipse导入jre方法
处理步骤:引入本机安装的jre1.8的步骤如下:
- 正确合理的建立MYSQL数据库索引
写在前面:索引对查询的速度有着至关重要的影响,理解索引也是进行数据库调优的起点.考虑如下情况,假设数据库中一个表有10^6条记录,DBMS的页面大小为4K,并存储100条记录.如果没有索引,查询将对整 ...
- $ cd `dirname $0` 和PWD%/* shell变量的一些特殊用法
在命令行状态下单纯执行 $ cd `dirname $0` 是毫无意义的.因为他返回当前路径的".". $0:当前Shell程序的文件名dirname $0,获取当前Shell程序 ...
- 理解ThreadLocal(一)
早在JDK 1.2的版本中就提供java.lang.ThreadLocal,ThreadLocal为解决多线程程序的并发问题提供了一种新的思路.使用这个工具类可以很简洁地编写出优美的多线程程序. Th ...
- 安装Ubuntu时的硬盘分区方案 转载
安装Ubuntu时的硬盘分区方案 http://www.cnblogs.com/shenliang123/p/3196743.html 如果你准备在硬盘里只安装Ubuntu一个操作系统的话,建议你采用 ...
- Big Number
问题陈述: 杭州电子科技大学 HANGZHOU DIANZI UNIVERSITY Online Judge Problem - 1018 问题解析: 公式一: n! = 10^m => lg( ...
- MYSQL delete 从多人表中删除
语法 1. delete table_list_A from table_list_B where bool_expression; 从delete_table_list_A 删除 bool expr ...
- 清理下NFC的基本概念
移动支付这事情热了总归还是会回归理性,就如同之前的10几年间的几次轮回一样.字面上看,移动支付比支付大也不大可能,有相同,有扩展,有交集有不通才是. NFC这事情也是说了快十年了,真心希望它能回归到其 ...
- openstack 使用cloud init 和 console-log, nbd或者libguestfs 获取VM中的硬件信息。
以获取PCI的信息为例. 基本代码: pci.py import base64 import guestfs from functools import partial import os impor ...
- 【LeetCode练习题】Copy List with Random Pointer
Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...