获取网页代码问题

Daimon 2010-04-08 02:50:20
我在写个小程序,需要过去网页代码的,我是菜鸟,请问我这个代码好吗?有不足的请指出!~~

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.Scanner;

public class URLReader
{
public static void main(String args[])
{
try
{
URL tirc = new URL("http://www.kkkmh.com/manhua/0609/mxo-mo-fa-xing-hui-mo-ai.html");
Scanner scan = new Scanner(tirc.openStream());
System.out.println(tirc.getHost());
while (scan.hasNext())
{
System.out.println(scan.next().replace("\\/", "/"));
}
}
catch (IOException e)
{
System.out.println(e);
}
}

public static String china(String args)
throws UnsupportedEncodingException
{
String s = null;
s = new String(args.getBytes("UTF-8"), "Unicode");
return s;

}

}
...全文
133 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
akqjxx 2010-04-11
  • 打赏
  • 举报
回复
,,这样也是有问题的,要看你访问网址的项目是放在什么服务器上的,如果服务器的编码是UTF8,那么在程序里面解析的时候就得用UTF8,在windows下,就得用GBK了。
ilrxx 2010-04-11
  • 打赏
  • 举报
回复
这不挺好了吗?就这样吧,没有了,没有了
alvin198761 2010-04-11
  • 打赏
  • 举报
回复
Java code
/** */
/**
* 读取一个网页全部内容
*/
public void getOneHtml(String htmlurl) throws Exception {
URL url;
String temp;
PrintWrite out =new PrintWrite(new File("D:/default.html"));
url = new URL(htmlurl);
BufferedReader in = new BufferedReader(new InputStreamReader(url
.openStream(), "utf-8"));// 读取网页全部内容
while ((temp = in.readLine()) != null) {
out.println(temp);
}
out.flush();
out.close();
in.close();
}

我写的事D:/default.html
你可以随便制定路径
Daimon 2010-04-11
  • 打赏
  • 举报
回复
指导一下吧............
Daimon 2010-04-10
  • 打赏
  • 举报
回复
有大大可以指导一下吗?
maxtomb 2010-04-09
  • 打赏
  • 举报
回复
Scanner scan = new Scanner(tirc.openStream(),"UTF-8")

Daimon 2010-04-09
  • 打赏
  • 举报
回复
感谢楼上的帮助,还有吗,还有吗?
coolbamboo2008 2010-04-08
  • 打赏
  • 举报
回复
mark
钱不是问题 2010-04-08
  • 打赏
  • 举报
回复

/** */
/**
* 读取一个网页全部内容
*/
public String getOneHtml(String htmlurl) throws Exception {
URL url;
String temp;
StringBuffer sb = new StringBuffer();

url = new URL(htmlurl);
BufferedReader in = new BufferedReader(new InputStreamReader(url
.openStream(), "utf-8"));// 读取网页全部内容
while ((temp = in.readLine()) != null) {
sb.append(temp);
}
in.close();
return sb.toString();
}
Daimon 2010-04-08
  • 打赏
  • 举报
回复
有高手能给点建议吗?
shuaiAWP 2010-04-08
  • 打赏
  • 举报
回复
乱码 好像
Daimon 2010-04-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 shuaiawp 的回复:]

乱码 好像
[/Quote]

Scanner scan = new Scanner(tirc.openStream(),"UTF-8")

改成這樣就可以了

62,624

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧