Jsoup,Java,Android Studio-无结果

I am currently writing an android app in android studio. I would like to connect with gog.com and search for a specific game. I have done this with steam and it works fine. When I want to search for a game at https://www.gog.com/games?page=1&search=game_title&sort=popularity it does not work.

我的代码片段:

String search ="https://www.gog.com/games?page=1&search=cyberpunk&sort=popularity";
Document doc = Jsoup.connect(search).get();
Elements titles = doc.getElementsByClass("product-tile__title");
for(Element title: titles){
    txt = title.text();
    Log.i("My_title", txt);
}

我默认写了“ cyberpunk”。该链接在浏览器中有效,但是当我使用Jsoup时,该链接返回“未找到结果”。 Cookie /用户代理/语言是否有问题?

评论
Henry
Henry

好吧,您是对的,您的代码应该可以正常工作,但在这种情况下,它将无法正常工作。

The reason is because if you disable javascript on your browser, and make this GET request https://www.gog.com/games?page=1&search=cyberpunk&sort=popularity you will see that the page will not load, and the reason is because these html elements are constructed or loaded by a javascript code.

So in this case you will have to parse the json that you received in your response instead of using JSOUP.

结论

由于JSOUP不是Web浏览器,因此无法运行javascript,因此在这种情况下,您将无法解析HTML响应,而只能解析HTML响应中的JSON。

点赞
评论