使用jsoup分析网页
来源:互联网 发布:域名服务器dns 编辑:程序博客网 时间:2024/05/18 02:06
之前用过HTMLParser,许久不更新的东西了,印象中也没那么好用。
今天重新搜索了一下,发现jsoup很容易上手~选择器很好很强大。
支持DOM和选择器两种模式
1、下载
jsoup的网站很简洁:http://jsoup.org/
入门做的很不错:http://jsoup.org/cookbook/
2、简单的例子
以下示例用于抓取iteye首页的新闻及连接,共使用了3种方式获取元素:
选择器用组件的Id用组件的class
今天重新搜索了一下,发现jsoup很容易上手~选择器很好很强大。
支持DOM和选择器两种模式
1、下载
jsoup的网站很简洁:http://jsoup.org/
入门做的很不错:http://jsoup.org/cookbook/
2、简单的例子
以下示例用于抓取iteye首页的新闻及连接,共使用了3种方式获取元素:
选择器用组件的Id用组件的class
- package tests;
- import java.io.IOException;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- import org.jsoup.nodes.Element;
- import org.jsoup.select.Elements;
- public class EgParseItEyeNews {
- public static void main(String[] args) throws IOException {
- String url = "http://www.iteye.com/";
- // 不加userAgent会被视为爬虫。。。。。
- Document doc = Jsoup.connect(url)
- .userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.2) Gecko/2008070208 Firefox/3.0.1 ")
- .get();
- // Element news = doc.getElementById("news"); //1、通过ID
- Elements newsConents=doc.getElementsByClass("news_content");//2、通过class
- Element news=newsConents.first();
- if (news == null)
- System.out.println(doc);
- else {
- // System.out.println(news);
- // System.out.println("end of news****************\n");
- Elements elems = news.select("a"); // 3、通过选择器 , 把链接都提取出来
- for (Element element : elems) {
- System.out.println(element.text() + " \t链接为:" + element.attr("href"));
- }
- }
- }
- }
package tests;import java.io.IOException;import org.jsoup.Jsoup;import org.jsoup.nodes.Document;import org.jsoup.nodes.Element;import org.jsoup.select.Elements;public class EgParseItEyeNews {public static void main(String[] args) throws IOException {String url = "http://www.iteye.com/";// 不加userAgent会被视为爬虫。。。。。Document doc = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.2) Gecko/2008070208 Firefox/3.0.1 ").get();//Element news = doc.getElementById("news"); //1、通过IDElementsnewsConents=doc.getElementsByClass("news_content");//2、通过classElement news=newsConents.first();if (news == null)System.out.println(doc);else {//System.out.println(news);//System.out.println("end of news****************\n");Elements elems = news.select("a"); // 3、通过选择器 , 把链接都提取出来for (Element element : elems) {System.out.println(element.text() + " \t链接为:" + element.attr("href"));}}}}
- 使用jsoup分析网页
- jsoup 分析网页数据
- 使用Jsoup解析网页
- Jsoup网页内容抓取分析
- android用jsoup分析网页
- Jsoup网页内容抓取分析
- jsoup网页内容抓取分析
- Jsoup网页内容抓取分析
- Android使用jsoup解析网页
- Android:使用jsoup解析网页
- 使用Jsoup解析html网页
- 使用Jsoup解析html网页
- Java使用Jsoup解析网页
- 使用jsoup采集网页实例
- 使用Jsoup解析html网页
- Jsoup网页内容抓取分析(1)
- 网页数据抓取——使用jsoup
- 使用Jsoup进行网页数据抓取
- 和我一起学 Selenium WebDriver(1)——入门篇
- Chrome:速度是第一要务
- Java读写操作
- Box2D v2.1.0用户手册翻译 - 目录,第01章 导言(Introduction)
- tslib-1.4的移植和应用
- 使用jsoup分析网页
- “没有错误”本身就是一个错误
- hdu 1010
- 转 Servlet生命周期与工作原理
- Q2_棋盘上的距离做题总结
- Java泛型-类型擦除
- URAL 1076 Trash 【最大权匹配KM快速模板O(N^3)】
- Comparator与Comparable的区别
- jvm GC日志输出