jsoup新闻爬虫

准备

jsoup-1.7.2.jar

jsoup中文API.docx(网上找)

工具类
JsoupTest.java

package newPaper;

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 JsoupTest {
    /**
     * 测试
     * 
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        // getWuMaoW();
        getNeiRong("http://news.ces.cn/huanbao/huanbaopinglun/20170505/123665_1.shtml");
    }

    // 获取指定网上的文章内容
    public static void getNeiRong(String url) {
        // String url = "http://www.ces.cn";
        Document doc = null;
        try {
            doc = Jsoup.connect(url).get();
            // first 为获取第一个元素
            Element first2 = doc
                    .getElementsByAttributeValue("class", "content").first();

            // 获取标题
            Element first = first2.getElementsByTag("h1").first();
            String title = first.text();
            System.out.println("标题:" + title);

            Element first3 = first2.getElementsByClass("content_nr").first();
            String ptext = first3.text();
            System.out.println(ptext);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // 获取指定网站上的 文章标题
    // 标题:正确计量碳排放对应对气候变化意义重大 
    // 链接:http://news.ces.cn/huanbao/huanbaopinglun/20170505/123665_1.shtml
    // 标题:美媒称中国将赢得新能源战争:美国悄然“让出”优势地位
    // 链接:http://news.ces.cn/xinnengyuan/xinnengyuanguoji/20170504/123664_1.shtml
    // 标题:关于征求《公共建筑节能改造节能量核定导则 
    // 链接:http://news.ces.cn/jianzhu/jianzhuzhengce/20170425/123662_1.shtml
    // 标题:重大突破:二氧化碳“变”汽油成真
    // 链接:http://news.ces.cn/qiche/qichejishu/20170505/123656_1.shtml
    // 标题:发改委发布节能标准体系建设方案 
    // 链接:http://news.ces.cn/zonghe/zonghezhengce/20170207/123154_1.shtml
    // 标题:能源互联网将成多能互补未来的发展趋势
    // 链接:http://news.ces.cn/zonghe/zonghezhengce/20170425/123601_1.shtml
    // 标题:两部委:全面推进城乡建筑节能绿色发展 
    // 链接:http://news.ces.cn/jianzhu/jianzhubaodao/20161226/122765_1.shtml
    // 标题:陈吉宁:大气、水、土“三大战役”怎么打?
    // 链接:http://news.ces.cn/huanbao/huanbaohuati/20170426/123576_1.shtml
    public static void getWuMaoW() {
        String url = "http://www.ces.cn";
        Document doc = null;
        try {
            doc = Jsoup.connect(url).get();
            // 根据属性 , 进行选择筛选
            Elements listDiv = doc.getElementsByAttributeValue("class",
                    "headline");

            for (Element element : listDiv) {
                Elements texts1 = element.getElementsByAttribute("href");
                for (Element text : texts1) {
                    String ptext = text.text();
                    String attr = text.attr("href");
                    System.out.println("标题:" + ptext);
                    System.out.println("链接:" + attr);
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于使用Java编写网络爬虫,我们可以使用Jsoup库来进行HTML解析和数据抓取。 首先,您需要在项目中引入Jsoup库。您可以在Maven或Gradle构建工具中添加以下依赖项: Maven: ```xml <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.13.1</version> </dependency> ``` Gradle: ```groovy implementation 'org.jsoup:jsoup:1.13.1' ``` 接下来,您可以使用Jsoup来获取和解析网页内容。以下是一个简单的示例代码,演示如何使用Jsoup来爬取网页并提取所需的数据: ```java import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; public class WebCrawler { public static void main(String[] args) { try { // 使用Jsoup连接到目标网页 Document document = Jsoup.connect("http://example.com").get(); // 使用CSS选择器提取所需的元素 Elements links = document.select("a[href]"); // 遍历并打印提取到的元素 for (Element link : links) { System.out.println("Link: " + link.attr("href")); System.out.println("Text: " + link.text()); } } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的示例中,我们使用`Jsoup.connect()`方法连接到目标网页,并使用`document.select()`方法使用CSS选择器提取所有带有`href`属性的链接元素。然后,我们遍历这些链接元素,并打印出链接的URL和文本。 您可以根据自己的需求进一步扩展和修改代码来满足特定的爬虫需求。请注意,在编写爬虫时,请遵守相关网站的使用条款和政策,以避免违反规定。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值