我正在尝试以编程方式搜索google&中的单词含义将其含义保存在计算机的文件中。我已经成功调用了这个页面&在Document(org.jsoup.nodes.Document)中获取响应。现在我不知道如何只从本文件中获得含义。请找到我已经指出我需要的数据部分的屏幕截图。
响应html太大了,我无法理解从哪个元素中获取我想要的数据。请帮忙。以下是我到目前为止所做的事情:
public class Search {
private static Pattern patternDomainName;
private Matcher matcher;
private static final String DOMAIN_NAME_PATTERN
= "([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}";
static {
patternDomainName = Pattern.compile(DOMAIN_NAME_PATTERN);
}
public static void main(String[] args) {
Search obj = new Search();
Set<String> result = obj.getDataFromGoogle("debug%20meaning");
for(String temp : result){
System.out.println(temp);
}
System.out.println(result.size());
}
public String getDomainName(String url){
String domainName = "";
matcher = patternDomainName.matcher(url);
if (matcher.find()) {
domainName = matcher.group(0).toLowerCase().trim();
}
return domainName;
}
private Set<String> getDataFromGoogle(String query) {
Set<String> result = new HashSet<String>();
String request = "https://www.google.com/search?q=" + query + "&num=20";
System.out.println("Sending request..." + request);
try {
// need http protocol, set this as a Google bot agent :)
Document doc = Jsoup
.connect(request)
.userAgent(
"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)")
.timeout(5000).get();
/**********Here comes my data fetching logic*****************
* Dont know where to fing my desired data in such a big html
*/
/*
String sc = doc.html().replaceAll("\\n", "");
System.out.println(doc.html());
*/
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
答案 0 :(得分:0)
Google Dictionary API已被弃用! 但是,相反,通过谷歌搜索URI,这是你目前正在做的事情,你可以使用这个http://google-dictionary.so8848.com/服务做同样的事情,最好更容易从你正在做的事情中抓取数据。