首页 > 解决方案 > java抓取缺少信息

问题描述

我目前正在自学网站爬行。我想有两种方法可以从网站获取 html 代码,一种使用 InputStreamReader,另一种使用 jsoup。我已经尝试过这两个,但似乎两者显示出不同的结果。

import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.net.*;
import java.io.*;

public class crawling {
    public static void main(String args[]) {
        try {
            BufferedReader buf;
            String line;
            URL url = new URL("http://www.example.com");
            buf = new BufferedReader(new InputStreamReader(url.openStream()));

            while(buf.readLine() != null) {
                line = buf.readLine();
                System.out.println(line);
                if(line.contains("background-color:")) {
                    line = line.replace("background-color:", " ");
                    System.out.println("I GOT IT: "+ line);
                }
            }

            buf.close();
            System.out.println("\n\nEnd of Streaming\nUse Jsoup\n\n");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Document doc;
        try {
            doc = Jsoup.connect("http://www.example.com").get();
            System.out.println(doc);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

如果我运行上面的代码,控制台会显示:

<html>
    <title>Example Domain</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    body {
        margin: 0;
        font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
}
        width: 600px;
        padding: 50px;
        border-radius: 1em;
    a:link, a:visited {
        text-decoration: none;
    @media (max-width: 700px) {
            background-color: #fff;
I GOT IT:               #fff;
        div {
            margin: 0 auto;
            padding: 1em;
    }
</head>
<body>
    <h1>Example Domain</h1>
    domain in examples without prior coordination or asking for permission.</p>
</div>
</html>


End of Streaming
Use Jsoup


<!doctype html>
<html>
 <head> 
  <title>Example Domain</title> 
  <meta charset="utf-8"> 
  <meta http-equiv="Content-type" content="text/html; charset=utf-8"> 
  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  <style type="text/css">
   body {
        background-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;

    }
    div {
        width: 600px;
        margin: 5em auto;
        padding: 50px;
        background-color: #fff;
        border-radius: 1em;
}
    a:link, a:visited {
        color: #38488f;
        text-decoration: none;
    }
    @media (max-width: 700px) {
        body {
             background-color: #fff;
        }
        div {
            width: auto;
            margin: 0 auto;
            border-radius: 0;
            padding: 1em;
        }
    }
    </style> 
 </head> 
<body> 
 <div> 
  <h1>Example Domain</h1> 
  <p>This domain is established to be used for illustrative examples in documents. You may use this domain in examples without prior coordination or asking for permission.</p> 
  <p><a href="http://www.iana.org/domains/example">More information...</a></p> 
  </div>   
 </body>
</html>

据此,使用 InputStreamReader 时似乎缺少一些信息......为什么会出现这种错误?
对我来说,使用 StreamReader 而不是 jsoup 似乎要容易得多。但由于它似乎缺少一些信息,我觉得我别无选择,只能使用 jsoup。

所以我想知道:
1.为什么使用输入流缺少一些信息,以及如何修复它。
2.如果推荐使用jsoup,我怎么能做和我使用输入流一样的事情,得到一个特定的字符串。我已经用谷歌搜索了它,但我在理解连接 url 后该怎么做时遇到了问题。

非常感谢您提前。

标签: javahtmlxmlweb-crawler

解决方案


您通过buf.readLine()在循环中调用两次来跳过备用行。

替换这个:

while(buf.readLine() != null) {
    line = buf.readLine();
    [...]

有了这个:

while((line = buf.readLine()) != null) {
    [...]

对于#2,JSoup 并不是真正做到这一点的方法,还有很多其他情况需要处理。但如果你仍然想这样做,有这种 hacky 方式:

Elements elems = doc.getElementsByTag("style"); //Select "style" element
for (Element elem : elems) {
    Node child = elem.childNode(0);
    String styleText = child.attr("data").replaceAll("background-color:\\s*#[a-f0-9]+;", ""); //Remove background color attribute
    child.attr("data", styleText); //Set the updated style back into the element
}
System.out.println(doc);

推荐阅读