java (Search Web) Modify Listing 12.18 WebCrawler.java to search for the word (e.g., Computer Programming) starting from a URL (e.g., http://cs.armstrong .edu/liang). Your program prompts the user to enter the word and the starting URL and terminates once the word is found. Display the URL for the page that contains the word.
时间: 2024-03-11 14:45:41 浏览: 154
java-sql-demo.zip_DEMO_java sql mysql De_java.sql. demo.
以下是修改后的WebCrawler.java代码,请仔细查看注释:
```java
import java.util.*;
import java.net.*;
import java.io.*;
public class WebCrawler {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//从用户输入中获取起始URL和要搜索的关键字
System.out.print("Enter the starting URL: ");
String startingURL = input.nextLine();
System.out.print("Enter the keyword to search for: ");
String keyword = input.nextLine();
//创建一个HashSet来存储已访问的URL
HashSet<String> urls = new HashSet<>();
//将起始URL添加到HashSet中
urls.add(startingURL);
//不断从HashSet中获取URL并解析它们
while (!urls.isEmpty()) {
String urlString = urls.iterator().next();
urls.remove(urlString);
//如果URL以"http"开头,则继续解析
if (!urlString.startsWith("http"))
continue;
//如果该URL已经被访问,则跳过
if (urls.contains(urlString))
continue;
//将该URL添加到已访问的URL集合中
urls.add(urlString);
try {
URL url = new URL(urlString);
Scanner urlInput = new Scanner(url.openStream());
//读取该URL的内容,并在其中查找关键字
while (urlInput.hasNext()) {
String line = urlInput.nextLine();
if (line.contains(keyword)) {
System.out.println("Keyword found at " + urlString);
return;
}
}
}
catch (Exception ex) {
System.out.println("Error: " + ex.getMessage());
}
}
System.out.println("Keyword not found.");
}
}
```
请注意,该程序会从用户输入中获取起始URL和要搜索的关键字,并不断地从已访问的URL列表中获取URL并解析它们,直到找到包含关键字的页面或没有更多的未访问的URL为止。如果找到了包含关键字的页面,则程序会打印该页面的URL并终止。如果没有找到包含关键字的页面,则程序会打印一条消息并终止。
阅读全文