Java URL扫描器换行
我有一个网站,我想读一些 我用的是扫描器,但在读整行之前它总是会断线 这是我的密码:Java URL扫描器换行,java,url,stream,Java,Url,Stream,我有一个网站,我想读一些 我用的是扫描器,但在读整行之前它总是会断线 这是我的密码: URL url = new URL("http://whereisthemonkey.weebly.com/better-mob-ai.html"); InputStream inputStream = url.openStream(); Scanner scanner = new Scanner(inputStream, "UTF-8"); //scanner
URL url = new URL("http://whereisthemonkey.weebly.com/better-mob-ai.html");
InputStream inputStream = url.openStream();
Scanner scanner = new Scanner(inputStream, "UTF-8");
//scanner.useDelimiter("\\n");
while(scanner.hasNext()){
String line = scanner.nextLine();
if(line.startsWith("<meta property=\"og:description\" content=\"I nformation")){
line = line.replace(" ", "").replace("┬", "").replace("á", "");
System.out.println(line);
line = line.substring(line.indexOf("Status:") + 7, line.indexOf("Status:") + 12);
int latestVersion = Integer.valueOf(line);
if(latestVersion == 0){
scanner.close();
inputStream.close();
System.err.println("/=============================================================================\\");
System.err.println("|[Better MobAI] The developing team of Better MobAI encountered a major error:|");
System.err.println("|[Better MobAI] The plugin will be therefore disabled! |");
System.err.println("\\============================================================================/");
return false;
}
if(latestVersion == 1){
scanner.close();
inputStream.close();
return true;
}
}
}
scanner.close();
inputStream.close();
URL=新URL(“http://whereisthemonkey.weebly.com/better-mob-ai.html");
InputStream InputStream=url.openStream();
扫描仪=新扫描仪(inputStream,“UTF-8”);
//scanner.useDelimiter(\\n”);
while(scanner.hasNext()){
字符串行=scanner.nextLine();
if(line.startsWith(“首先:我从您的站点获取所有HTML内容,如下所示:
昨天,我只找到了一个“Status”单词。因此,您在if语句中的条件不正确,因为行中不存在该单词
今天,(网站更新)我发现了两个“状态”字。所以,你在if语句中的条件是正确的,哪一行包含这个字。你可以将endIndex
更改为line.indexOf(“状态:”)+8
。另一个“状态”“word将忽略,因为您的条件latestVersion===\uuu
为true
然后返回
并中断循环
但是等等..这样对我来说很不舒服,因为网站每次都会刷新。所以,你的条件不可能正常工作
因此,我建议您对它读取的每一行使用string.contains(“Status”);
。如下所示:
public static boolean latestVersion() throws Exception {
URL url = new URL("http://whereisthemonkey.weebly.com/better-mob-ai.html");
InputStream inputStream = url.openStream();
Scanner scanner = new Scanner(inputStream, "UTF-8");
int numLine = 0;
while (scanner.hasNext()) {
String line = scanner.nextLine();
numLine++;
String status = "-1"; // equal any number like -1 which Status will never equal it
if (line.contains("Status")) {
int indexOfStatus = line.indexOf("Status");
status = line.substring(indexOfStatus + 7, indexOfStatus + 9);
System.out.println("line " + numLine + ": contains Status word | Status = " + status);
}
// use trim to avoid any spaces
int latestVersion = Integer.parseInt(status.trim());
if (latestVersion == 0) {
scanner.close();
inputStream.close();
System.err.println("/=============================================================================\\");
System.err.println("|[Better MobAI] The developing team of Better MobAI encountered a major error:|");
System.err.println("|[Better MobAI] The plugin will be therefore disabled! |");
System.err.println("\\============================================================================/");
return false;
}
if (latestVersion == 1) {
System.out.println("latestVersion: " + latestVersion);
scanner.close();
inputStream.close();
return true;
}
}
scanner.close();
inputStream.close();
return false;
}
只需提示:任何连接到internet网络的连接都使用线程
,以确保您的数据全部下载,并且可能需要很长时间。整行内容是什么样子的?@MinhKieu我不知道,我使用weebly作为主机,无法控制网站。当然,您可以使用浏览器访问网站并查看html-来源?不,这在weebly上是不可能的,我已经试过了,你怎么知道它没有读完整的一行,但中途中断了?
public static boolean latestVersion() throws Exception {
URL url = new URL("http://whereisthemonkey.weebly.com/better-mob-ai.html");
InputStream inputStream = url.openStream();
Scanner scanner = new Scanner(inputStream, "UTF-8");
int numLine = 0;
while (scanner.hasNext()) {
String line = scanner.nextLine();
numLine++;
String status = "-1"; // equal any number like -1 which Status will never equal it
if (line.contains("Status")) {
int indexOfStatus = line.indexOf("Status");
status = line.substring(indexOfStatus + 7, indexOfStatus + 9);
System.out.println("line " + numLine + ": contains Status word | Status = " + status);
}
// use trim to avoid any spaces
int latestVersion = Integer.parseInt(status.trim());
if (latestVersion == 0) {
scanner.close();
inputStream.close();
System.err.println("/=============================================================================\\");
System.err.println("|[Better MobAI] The developing team of Better MobAI encountered a major error:|");
System.err.println("|[Better MobAI] The plugin will be therefore disabled! |");
System.err.println("\\============================================================================/");
return false;
}
if (latestVersion == 1) {
System.out.println("latestVersion: " + latestVersion);
scanner.close();
inputStream.close();
return true;
}
}
scanner.close();
inputStream.close();
return false;
}