【发布时间】:2016-04-26 22:10:20
【问题描述】:
我正在尝试使用 JSoup,但无法让我的 Scanner 进行第二次复飞。它直接跳到我的 catch 语句。
这是程序的描述:
我将谷歌搜索词作为用户输入(字符串)。接下来,我询问用户希望查看的查询项的数量,并输入一个整数。
我遍历返回的每个元素并将其添加到 ArrayList。控制台上显示的字符串由索引、链接文本和超链接组成。
然后我想询问用户他们想进入哪个索引以打开指向该链接的浏览器窗口。这是通过使用 Runtime 类将 hRef 字符串与 Linux 终端命令“xdg-open”共同连接来完成的。
在询问将选择哪个索引之前,它运行良好。
这是我的代码:
/**
* Created by christopher on 4/26/16.
*/
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class GoogleSearchJava {
static int index;
static String linkHref;
static Scanner input;
public static final String GOOGLE_SEARCH_URL = "https://www.google.com/search";
public static void main(String[] args) throws IOException {
//GET INPUT FOR SEARCH TERM
input = new Scanner(System.in);
System.out.print("Search: ");
String searchTerm = input.nextLine();
System.out.print("Enter number of query results: ");
int num = input.nextInt();
String searchURL = GOOGLE_SEARCH_URL + "?q=" + searchTerm + "&num=" + num;
//NEED TO DEFINE USER AGENT TO PREVENT 403 ERROR.
Document document = Jsoup.connect(searchURL).userAgent("Mozilla/5.0").get();
//OPTION TO DISPLAY HTML FILE IN BROWSWER. DON'T KNOW YET.
//System.out.println(doc.html());
//If google search results HTML change the <h3 class="r" to <h3 class ="r1"
//need to change below stuff accordingly
Elements results = document.select("h3.r > a");
index = 0;
String news = "News";
ArrayList<String> displayResults = new ArrayList<>();
for (Element result : results) {
index++;
linkHref = result.attr("href");
String linkText = result.text();
String pingResult = index + ": " + linkText + ", URL:: " + linkHref.substring(6, linkHref.indexOf("&")) + "\n";
if (pingResult.contains(news)) {
System.out.println("FOUND " + "\"" + linkText + "\"" + "NO HYPERTEXT FOR NEWS QUERY RESULTS AT THIS TIME. SKIPPED INDEX.");
System.out.println();
} else {
displayResults.add(pingResult);
}
}
for(String urlString : displayResults) {
System.out.println(urlString);
}
System.out.println();
goToURL(input, displayResults);
}
public static int goToURL(Scanner input, ArrayList<String> resultList) {
int newIndex = 0;
try {
System.out.print("Enter Index (i.e. 1, 2, etc) you wish to visit, 0 to exit: ");
newIndex = input.nextInt();
input.nextLine();
for (String string : resultList) {
if(string.startsWith(String.valueOf(newIndex))) {
Process process = Runtime.getRuntime().exec("xdg-open " + string.substring(6, string.indexOf("&")));
process.waitFor();
}
}
} catch (Exception e) {
System.out.println("ERROR while parsing URL");
}
return newIndex;
}
}
这里是输出注意我输入“1”后它是如何停止的 不,我还没有注意按“0”:
Search: Oracle
Enter number of query results: 3
1: Oracle | Integrated Cloud Applications and Platform Services, URL:: =http://www.oracle.com/
2: Oracle Corporation - Wikipedia, the free encyclopedia, URL:: =https://en.wikipedia.org/wiki/Oracle_Corporation
3: Oracle on the Forbes America's Best Employers List, URL:: =http://www.forbes.com/companies/oracle/
Enter Index (i.e. 1, 2, etc) you wish to visit, 0 to exit: 1
ERROR while parsing URL
Process finished with exit code 0
【问题讨论】:
-
另请参阅When Runtime.exec() won't,了解有关正确创建和处理流程的许多好技巧。然后忽略它引用
exec并使用ProcessBuilder创建进程。
标签: java jsoup java.util.scanner runtime.exec