Jsoup实现新闻网页的爬取,标题,正文,图片,新闻时间,网页链接的解析示例

以下代码为一个Jsoup爬取新闻网页的简单示例,可直接运行。

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class newCrawl {
  public LinkedList ImgUrls=new LinkedList();//用于存放图片URL
  public LinkedList linkurls=new LinkedList();//用于存放url链接

  public static void main(String[] args) throws IOException {  
    newCrawl newCl = new newCrawl();   
    String url="http://world.huanqiu.com/article/2016-01/8412590.html?from=bdwz";
            Document doc = Jsoup.connect(url).get();
            newCl.downloadPage(url);//下载网页 
            String title=newCl.getnewTitle(doc);//获取新闻标题
            String time=newCl.getTime(doc);//获取新闻发布时间
            String text=newCl.getNewtext(doc);//获取新闻内容
    System.out.println("新闻Url:"+url);
    System.out.println("新闻标题:"+title);
    //System.out.println("newsTime:"+time);
    System.out.println("新闻内容:"+text);
    System.out.println("******************************************************************");
    newCl.getImgu
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用Jsoup分页数据的步骤如下: 1. 定义要网页地址和需要的内容。 2. 使用Jsoup网页,获网页内容。 3. 使用Jsoup解析网页内容,提需要的数据。 4. 如果网页中存在多页数据,使用循环遍历所有网页,重复步骤2和步骤3。 5. 将提的数据保存到本地或数据库中。 下面是一个示例代码,演示如何使用Jsoup分页数据: ```java 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 PageCrawler { public static void main(String[] args) throws IOException { // 定义要网页地址和需要的内容 String url = "https://example.com/page/1"; String cssSelector = ".list-item"; // 使用Jsoup网页,获网页内容 Document doc = Jsoup.connect(url).get(); // 使用Jsoup解析网页内容,提需要的数据 Elements items = doc.select(cssSelector); for (Element item : items) { // 处理每个数据项 String title = item.select(".title").text(); String description = item.select(".description").text(); System.out.println(title); System.out.println(description); System.out.println("------------"); } // 如果网页中存在多页数据,使用循环遍历所有网页 for (int i = 2; i <= 10; i++) { String nextUrl = "https://example.com/page/" + i; doc = Jsoup.connect(nextUrl).get(); items = doc.select(cssSelector); for (Element item : items) { // 处理每个数据项 String title = item.select(".title").text(); String description = item.select(".description").text(); System.out.println(title); System.out.println(description); System.out.println("------------"); } } // 将提的数据保存到本地或数据库中 // ... } } ``` 在示例代码中,我们首先定义了要网页地址和需要的内容。 然后,我们使用Jsoup网页,获网页内容,并使用Jsoup解析网页内容,提需要的数据。 如果网页中存在多页数据,我们使用循环遍历所有网页,重复步骤2和步骤3。 最后,我们可以将提的数据保存到本地或数据库中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值