【问题标题】:how can i get all the hyperlinks and its paragraphs in an website?如何获取网站中的所有超链接及其段落?
【发布时间】:2015-12-10 08:41:26
【问题描述】:

我想获取所有超链接并将其命名为 .txt 文件,我想将所有段落存储在每个超链接中,并按文章标题另存为文本文件。

我在这里有代码,我正在修复这个问题 2 个月。我无法获取此爬行/抓取逻辑的代码。 请任何人编码并修复它。

  import java.io.FileOutputStream;
    import java.io.IOException;
     import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
   import java.io.Reader;
   import java.net.URI;
   import java.net.URISyntaxException;
    import java.net.URL;
     import java.net.URLConnection;

      import javax.swing.text.BadLocationException;
     import javax.swing.text.EditorKit;
     import javax.swing.text.html.HTMLDocument;
   import javax.swing.text.html.HTMLEditorKit;

     import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
      import org.jsoup.nodes.Element;
     import org.jsoup.select.Elements;

     public class App {
     public static void main(String[] args) throws URISyntaxException,
        IOException, BadLocationException {
    HTMLDocument doc = new HTMLDocument() {
        public HTMLEditorKit.ParserCallback getReader(int pos) {
            return new HTMLEditorKit.ParserCallback() {
                public void handleText(char[] data, int pos) {
                    System.out.println(data);
                }
            };
        }
    };

    URL url = new URI("http://tamilblog.ishafoundation.org/").toURL();
    URLConnection conn = url.openConnection();
    Reader rd = new InputStreamReader(conn.getInputStream());
    OutputStreamWriter writer = new OutputStreamWriter(
            new FileOutputStream("ram.txt"), "UTF-8");

    EditorKit kit = new HTMLEditorKit();
    kit.read(rd, doc, 0);
    try {
        Document docs = Jsoup.connect(
                "http://tamilblog.ishafoundation.org/").get();

        Elements links = docs.select("a[href]");

        Elements elements = docs.select("*");
        System.out.println("Total Links :" + links.size());

        for (Element element : elements) {
            System.out.println(element.ownText());
        }
        for (Element link : links) {
            String hrefUrl = link.attr("href");
            if (!"#".equals(hrefUrl) && !hrefUrl.isEmpty()) {
                System.out.println(" * a: link :" + hrefUrl);
                System.out.println(" * a: text :" + link.text());
                Document document = Jsoup.connect(hrefUrl)
                        .timeout(0) //Infinite timeout
                        .get();
                String html = document.toString();
                writer.write(html);
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        writer.close();
    }
}
 }

【问题讨论】:

标签: java web-scraping web-crawler jsoup urlconnection


【解决方案1】:

试试这样的

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 NewClass {


  public static void main(String[] args) throws IOException {


      Document doc = Jsoup.connect("http://tamilblog.ishafoundation.org").get();
      Elements section = doc.select("section#content");
      Elements article = section.select("article");
      for (Element a : article) {
        System.out.println("Title : \n" + a.select("a").text());
        System.out.println("Article summary: \n" + a.select("div.entry-summary").text());
      }
    }


}

【讨论】:

  • 你能简单解释一下你在做什么吗?
  • 我的英语不好,但我会试着告诉我我做了什么。 1. Jsoup.connect("url") 连接到页面 2. select("cssQuery") 从页面中选择您感兴趣的部分。例如 select("h2") 将返回页面的所有 h2 元素; select ("h2#someId") 返回一个 id = someId 的 h2 元素
  • and select("div.entry-summary") 返回类entry-summary中的所有div
  • @MrHug :它称为网络爬取/抓取,我正在使用 java 中的 url 连接器编写代码。我想使用 java 中的 jsoup 从网站中提取内容。就像我们可以提取的所有
  • @user5664421:谢谢我想要这种类型的代码。但它正在从主页获取超链接的标题和内容。我想使用一些迭代器并获取主页中的所有文章链接正在重定向到其他超链接。类似于广度优先搜索算法。
猜你喜欢
相关资源
最近更新 更多
热门标签