程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

java爬取缺少信息

发布于2023-12-26 22:14     阅读(888)     评论(0)     点赞(5)     收藏(0)


我目前正在自学网站爬行。我认为有两种方法可以从网站获取 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,我怎样才能做与使用输入流相同的事情,获取特定的字符串。我已经用谷歌搜索过了,但我在理解连接网址后要做什么时遇到了问题。

预先非常感谢您。


解决方案


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

替换这个:

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

有了这个:

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

对于#2,JSoup 并不是真正的方法,您还需要处理许多其他情况。但如果你仍然想这样做,有一个很奇怪的方法:

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);



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.qianduanheidong.com/blog/article/533295/3af02143a59d38c7f9e3/

来源:前端黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

5 0
收藏该文
已收藏

评论内容:(最多支持255个字符)