Jsoup:select(div [class = rslt prod])不应返回null [英] Jsoup: select(div[class=rslt prod]) returns null when it shouldn't

查看:98
本文介绍了Jsoup:select(div [class = rslt prod])不应返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从此页面中选择所有带有class ="rlts prod"的div http://www.amazon.fr/s/field-keywords=samsung

I am trying to select the all div with class="rlts prod" from this page http://www.amazon.fr/s/field-keywords=samsung

Document doc = Jsoup.connect("http://www.amazon.fr/s/field-keywords=samsung").get();
Elements divProd = doc.select("div[class=rslt prod]");      
System.out.println("\nsize: "+divProd.size());

但是它返回0并且不应该,为什么呢?

But it returns 0 and it shouldn't, any idea why ?

应选择的示例:

<div id="result_4" class="rslt prod" name="B006O9QNHU">
[...]
</div>

推荐答案

您必须更改用户代理,否则您将从亚马逊获得一个不同的网站.

You have to change the user agent, otherwise you get a differnt website from amazon.

Document doc = Jsoup.connect("http://www.amazon.fr/s/field-keywords=samsung")
        .userAgent("Mozilla/17.0") // you can use any other user agent here
        .get();

for( Element element : doc.select("div[class=rslt prod]") )
{
    System.out.println(element);
    System.out.println("");
}

现在输出的是类似列表

<div id="result_1" class="rslt prod" name="B007XOM6SU"> 
  ...
</div>

<div id="result_2" class="rslt prod" name="B006SXSF4Q"> 
  ...
</div>

...

这篇关于Jsoup:select(div [class = rslt prod])不应返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆