如何在Google Video Search上获得第一个网址结果? (标签选择器)

时间:2018-04-16 21:07:31

标签: java html tags jsoup

我想使用JSoup以编程方式获取第一个URL结果谷歌视频搜索。我对Google视频编码或Html标记有疑问。(可能是HTML标记:.g> .r> a)

public static String getYoutubeURLByName(String search) throws UnsupportedEncodingException, MalformedURLException, MalformedURLException, MalformedURLException, MalformedURLException, IOException {
    String google = "https://www.google.com/videohp?hl=";
    String charset = "UTF-8";
    String userAgent = "Mozilla/5.0"; 
    Elements links = Jsoup.connect(google + URLEncoder.encode(search, charset)).userAgent(userAgent).get().select(".g>.r>a");
    String url = links.get(0).absUrl("href");
    url = URLDecoder.decode(url.substring(url.indexOf('=') + 1, url.indexOf('&')), "UTF-8");
    return url;
}

我收到以下错误。我认为Jsoup.connect无法在数组列表中添加Element,因为编码url或html标记有问题。 (可能是html标签,我可怜的html :()

  

at java.util.ArrayList.rangeCheck(ArrayList.java:657)   at java.util.ArrayList.get(ArrayList.java:433)   at getYoutubeURLByName(.java:100)

不可能重复:How can you search Google Programmatically Java API

重要编辑: 而字符串谷歌应该是:

https://www.google.com/search?tbm=vid&hl=en-TR&source=hp&biw=&bih=&q=

1 个答案:

答案 0 :(得分:1)

您的选择器似乎不正确,因为元素不是直接子节点,请尝试使用:

Elements links = Jsoup.connect(google + URLEncoder.encode(search, charset)).userAgent(userAgent).get().select(".g .r a");