我尝试解析谷歌的搜索结果。我需要的不是搜索结果本身,而是搜索结果是否存在的信息!
现在我的问题是我想搜索组合字符串。例如。 “最大测试员”。 现在谷歌真的很好,告诉我: 我们找不到“Max Testperson”的搜索结果,而是找到“Max Testperson”的搜索结果。但 !!!我不需要 Max Testperson,我需要“Max Testperson”。
所以基本上我对搜索结果本身不感兴趣,而是对搜索结果之前的部分感兴趣(是否可以找到搜索字符串!)。
我在 java 中使用了以下教程: http://mph-web.de/web-scraping-with-java-top-10-google-search-results/
有了这个我可以解析搜索结果。但就像我说的!没必要!我只想知道我的搜索字符串是否存在。由于谷歌删除了 ->""<- 我无论如何都会得到搜索结果。
谁能帮我解决这个问题?
最佳答案
尝试将获取参数 nfpr=1
添加到您的搜索中以禁用自动更正功能:
final Document doc = Jsoup.connect("https://google.com/search?q=test"+"&nfpr=1").userAgent(USER_AGENT).get();
更新:
您可以解析有关无结果的消息:
public class App {
public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
public static void main(String[] args) throws Exception {
String searchTerm = "\"daniel+nasseh\"+\"26.02.1987\"";
boolean hasExactResults = true;
final Document doc = Jsoup.connect("https://google.com/search?q=" + searchTerm + "&nfpr=1")
.userAgent(USER_AGENT).get();
Elements noResultMessage = doc.select("div.e.obp div.med:first-child");
if (!noResultMessage.isEmpty()) {
hasExactResults = false;
for (Element result : noResultMessage) {
System.out.println(result.text());
}
}
if (hasExactResults) {
// Traverse the results
for (Element result : doc.select("h3.r a")) {
final String title = result.text();
final String url = result.attr("href");
System.out.println(title + " -> " + url);
}
}
}
}
更新 2:Donselm 本人在评论中提出的最佳解决方案是添加 &tbs=li:1
以强制搜索准确的搜索词
String searchTerm = "\"daniel+nasseh\"+\"26.02.1987\"";
final Document doc = Jsoup.connect("https://google.com/search?q=" + searchTerm + "&tbs=li:1").userAgent(USER_AGENT).get();
关于java - 获取谷歌搜索结果是否存在的信息(JAVA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37268406/