我试图实现程序读取网页源代码并将其保存在文本文件中,然后在其中做一些操作,但是当我读取网页源代码时,原有的网页源代码与java程序的网页源代码的输出存在差异。
我的节目:
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();我在这行源代码中的问题
原版网页源代码,第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;" />java程序的实际输出:
<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;" />对这个问题有什么建议吗?
发布于 2017-01-19 11:01:58
您必须设置用户-代理:
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");完整代码:
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();结果
<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
复制相似问题