2016-09-24 71 views
-1

我们目前正在从事一个有jsoup概念的项目。我们无法检索使用Yahoo和Bing搜索的搜索关键字的链接,这可能会在Google搜索引擎中实现。我们正在使用servlet和html。你能找到解决方案吗?检索使用雅虎和Bing搜索的链接

public static final String GOOGLE_SEARCH_URL = "https://www.google.com/search"; 
String searchURL = GOOGLE_SEARCH_URL + "?q="+word+"&num="+num; 
Document doc = Jsoup.connect(searchURL).userAgent("Mozilla/5.0").get(); 
Elements result = doc.select("h3.r a"); 
for (Element res : result) { 
    String linkHref = res.attr("href"); 
    linkHref = linkHref.substring(7, linkHref.indexOf("&")); 
    out.println("<a href="+linkHref+">"+linkHref+"</a>"); 

上述程序正在检索使用谷歌搜索时的链接。但是,当我们将url更改为Bing和Yahoo时,它不会检索链接。

+0

然后检查Yahoo和Bing的HTML源代码,看看它为什么不起作用。有可能他们使用的代码会隐藏JSoup等简单分析器的链接。 –

回答

2

1)你试图达到结果的方式是错误的。 重要提示:所有这三个搜索引擎都有不同的显示结果的方式。

2)您应该检查并找出在通过浏览器触发任何搜索查询后显示的HTML。使用浏览器控制台检查元素。

每当我们在这三个搜索引擎,我们得到不同形式的结果搜索任何东西:

在冰

<li class="b_algo" data-bm="8"> 
 
    <h2><a href="https://en.wikipedia.org/wiki/Keanu_Reeves" h="ID=SERP,5133.1"><strong>Keanu Reeves</strong> - <strong>Wikipedia</strong>, the free encyclopedia</a></h2> 
 
    <div class="b_caption"> 
 
    <div class="b_attribution"><cite>https://<strong>en.wikipedia.org</strong>/wiki/<strong>Keanu_Reeves</strong></cite> 
 
    </div> 
 
    <p><strong>Keanu</strong> Charles <strong>Reeves</strong> (/ k eɪ ˈ ɑː n uː/kay-AH-noo [citation needed]; born September 2, 1964) is a Canadian actor, producer, director and musician.</p> 
 
    </div> 
 
</li>

在雅虎

<div class="compTitle options-toggle"> 
 
    <h3 class="title"><a class=" td-u" data-sb="/beacon/clk;_ylt=A2oKmK.cOOlXskYAmKe7HAx.;_ylu=X3oDMTBycWJpM21vBGNvbG8Dc2czBHBvcwMxBHZ0aWQDBHNlYwNzcg--" href="https://en.wikipedia.org/wiki/Keanu_Reeves" referrerpolicy="origin" target="_blank" data-cff="57e9389d37daf"><b>Keanu</b> Reeves - Wikipedia, the free encyclopedia</a></h3> 
 
    <div><span class=" fz-ms fw-m fc-12th wr-bw">en.wikipedia.org/wiki/<b><b>Keanu</b></b>_Reeves</span> 
 
    </div> 
 
</div>

在谷歌

<h3 class="r"><a href="https://en.wikipedia.org/wiki/Keanu_Reeves" onmousedown="return rwt(this,'','','','1','AFQjCNHeNQLRv6isQkhVWpt6-1ftD0Q0vw','EZmLIYbQoBakBQ8oWWstdQ','0ahUKEwjz_KKbp63PAhVKrY8KHVnuBPUQFggcMAA','','',event)">Keanu Reeves - Wikipedia, the free encyclopedia</a></h3>

所以这是不可能的你PROGRAMM Elements result = doc.select("h3.r a");到findout结果对所有的搜索引擎。

3)限制搜索结果的方式也不正确。你必须为每个人使用不同的URL query

谷歌:use num=1 - https://www.google.com/search?q=test&num=1
兵:use count=1 - http://www.bing.com/search?q=test&count=1
雅虎:use n=1 - https://search.yahoo.com/search?p=test&n=1

当使用BingSearchURL你可以这样做如下:

public static final String BING_SEARCH_URL = "https://www.bing.com/search"; 

String searchURL = BING_SEARCH_URL + "?q=" + word + "&count=" + num; 
    Document doc = Jsoup.connect(searchURL).userAgent("Mozilla/5.0").get(); 
    Elements result = doc.select("li.b_algo h2 a"); 
    for (Element res : result) { 
     String linkHref = res.attr("href"); 
     //linkHref = linkHref.substring(7, linkHref.indexOf("&")); //No need of doing substring 
     System.out.println("<a href=" + linkHref + ">" + linkHref + "</a>"); 
    }