reading web page source code in java Differs from the orginal webpage source code

这一生的挚爱 提交于 2019-12-25 16:44:35

问题


I am trying to implement program to read webpage source code and save it in text file then do some operations in it but the problem when I read web page source code , there is difference between the orginal web page source code and the output of java program web page source code.

my program :

        String inputLine;
        URL link = new URL("http://www.ammanu.edu.jo/English/Articles/newsArticle.aspx?id=2935");
        BufferedReader in = new BufferedReader( new InputStreamReader(link.openStream(),"UTF-8"));

        while ((inputLine = in.readLine()) != null){
                System.out.println(inputLine);
        }
        in.close();

and my problem in this line of source code

orginal webpage source code at line 1156 :

<img id="img" src="http://www.ammanu.edu.jo/AdminImages/20652935/_DSC0246.jpg" style="height:310px;width:400px;border-width:0px;display:block;float:left; padding-right:5px;" />

Actual output of java program :

<img id="img" src="http://www.ammanu.edu.jo/AdminImages/20652935/_DSC0246.jpg" height="310" width="400" border="0" style="display:block;float:left; padding-right:5px;" />

any suggested solution for this problem ?


回答1:


You have to set the user the User-Agent:

con.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");

Full code:

    String inputLine;
    URL link = new URL("http://www.ammanu.edu.jo/English/Articles/newsArticle.aspx?id=2935");
    URLConnection con = link.openConnection();
    con.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");

    BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream(),"UTF-8"));

    while ((inputLine = in.readLine()) != null){
        System.out.println(inputLine);
    }
    in.close();

Result

<img id="img" src="http://www.ammanu.edu.jo/AdminImages/20652935/_DSC0246.jpg" style="height:310px;width:400px;border-width:0px;display:block;float:left; padding-right:5px;" />


来源:https://stackoverflow.com/questions/41739578/reading-web-page-source-code-in-java-differs-from-the-orginal-webpage-source-cod

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!