简体   繁体   English

使用Jsoup获取HTML标签

[英]Get HTML tag using Jsoup

I am using Jsoup to get Amazon item data from a URL and need to iterate through item tags in the document, but I cannot find the tag. 我正在使用Jsoup从URL获取Amazon项目数据,并且需要遍历文档中的item标签,但是我找不到该标签。

An example URL can be found here . 可以在此处找到示例URL。 What am I doing wrong? 我究竟做错了什么?

Please search and post code in the future, it took me a few seconds to find an appropriate set of answers for this question. 以后请搜索并发布代码,我花了几秒钟时间才找到该问题的适当答案。

Sources: 资料来源:

I (...) need to iterate through item tags in the document, but I cannot find the tag. 我(...)需要遍历文档中的项目标签,但是找不到标签。

There's no tag item . 没有标签item Instead it's a div element with s-item-container class. 相反,它是带有s-item-container类的div元素。

SAMPLE CODE 样本代码

Document doc = Jsoup //
                .connect("http://www.amazon.com/s/ref=nb_sb_ss_i_1_4?url=search-alias%3Daps&field-keywords=clash+royale&sprefix=clas%2Caps%2C288&rh=i%3Aaps%2Ck%3Aclash+royale") //
                .get();

for (Element item : doc.select("div.s-item-container")) {
     System.out.println("-----");
     System.out.println("Title: " + item.select("a.s-access-detail-page").attr("title"));
     System.out.println("Customer Reviews Count: " + item.select("a[href$=#customerReviews]").text());

     // Extract other data...
}

OUTPUT (stripped) 输出(剥离)

-----
Title: 
Customer Reviews Count: 
-----
Title: Impact ryale
Customer Reviews Count: 1
-----
Title: Castle Clash: Age of Legends
Customer Reviews Count: 6,001
-----
Title: Geometry Dash Meltdown
Customer Reviews Count: 2,924
(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM