1

有没有办法让结果链接显示确切的链接,而不是 bing 重定向?

例如,在 bing 认知搜索中有一种方法可以将 href 置于实际链接而不是重定向?

`"value": [
 {
     "id": "https://api.cognitive.microsoft.com/api/v5/#WebPages.0",
     "name": "<b>Burrito Recipes</b> - Allrecipes.com",
     "url": "http://www.bing.com/cr?IG=4BE4CA19570B4740ABE2B85782727544&CI......",
     "displayUrl": "all<b>recipes</b>.com/<b>recipes</b>/1216",
     "snippet": "<b>Burrito Recipes</b> ...",
     "deepLinks": [
         {
             "name": "Mexican",
             "url": "http://www.bing.com/cr?IG=4BE4CA19570B4740ABE2BF...",
             "snippet": "Mexican Recipes Find ..."
         }
      ]`

所以 url 是实际的链接?

4

2 回答 2

1

您可以在 url 中使用 'r=' 查询参数来获取重定向所指向的确切 url。一个示例 bing url 看起来像这样

"url": "http://www.bing.com/cr?IG=584DA9A5C8B245DDA12848B177BAF817&CID=138CDEBD227860443797D43D239D616C&rd=1&h=0UIFjW9hoNBrd0LFRhopxM1IDwbKCHvSc-z-FdLfyMQ&v=1&r=http%3a%2f%2fallrecipes.com%2frecipes%2f1216%2fworld-cuisine%2flatin-american%2fmexican%2fmain-dishes%2fburritos%2f&p=DevEx,5066.1"

这里,

r=http%3a%2f%2fallrecipes.com%2frecipes%2f1216%2fworld-cuisine%2flatin-american%2fmexican%2fmain-dishes%2fburritos%2f

请注意,url 是编码的。如果需要,执行 url 解码。所有这些都可以使用以下 Java 代码来完成。

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URIBuilder;
import org.apache.log4j.Logger;

import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public static String getExactUrl(String url) {
    Map<String, String> queryParams = getQueryParamsMap(url);
    return queryParams.getOrDefault("r", url);
}

public static Map<String, String> getQueryParamsMap(String url) {
    Map<String, String> queryParamsMap = new HashMap<>();
    try {
        URIBuilder uriBuilder = new URIBuilder(url);
        List<NameValuePair> queryParams = uriBuilder.getQueryParams();
        queryParamsMap = queryParams.stream().collect(Collectors.toMap(NameValuePair::getName, NameValuePair::getValue));
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    return queryParamsMap;
}
于 2017-05-12T14:07:37.160 回答
0

我们发现的唯一方法是删除<b></b>displayUrl...

于 2017-04-11T20:41:14.220 回答