Scrape youtube href using jsoup


April 2019


226 time


I'm using jsoup in java and I'm trying to scrape the first href in a particular youtube video search. However, I can't figure out the correct css query in order to obtain the href. If someone can point me in the correct direction, that'd be great. Here is the image of the html I'm trying to scrape on youtube.

The following is one of the selects I've tried, but doesn't print out anything.

My code:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class WebTest
    public static void main(String[] args)
        try {
            Elements musicVideoLink =" a[href]");

            String linkh = musicVideoLink.attr("href");
        catch (IOException ex){ }

1 answers


With the JSoup.connect().get(), because there are no other headers in the request like User-Agent, YouTube returns quite a basic HTML rendering of the search results. This is quite different from the structure in the linked image above, but actually easier to select in though:

Elements musicVideoLink =" a");

This looks like the easiest solution here. If you do pass in the User-Agent header, you get back the same as the Network tab in the browser inspector shows, but this doesn't yet match the result in that image. The browser clearly does a bit of AJAX style processing and rendering on that response next.