简体   繁体   English

如何使用JSoup从HTML元素中选择具有特定文本的元素

[英]How to select an element with specific text from the HTML a element using JSoup

I got an a element with few attributes one of them is data-product-id this is my element that I want. 我有一个attributes很少a元素,其中一个是data-product-id这是我想要的元素。

for example data-product-id="002212" I am intrested in the number "002212" 例如data-product-id="002212"我被编号"002212"

My problem is that there can be couple a elements with this link 我的问题是,有可能是夫妇a与该链接元素

There is how link looks like. 有链接的样子。

<a href="something.com" title="test tile" class="title-product" data-jsevent="obj:title--product" data-product-name="test" data-product-id="002212" ddata-product-price="1.99" data-product-brand="test"  data-product-quantity="1"> 

I did something like this: 我做了这样的事情:

Elements links = document.select("a.title-product");

I receives every a element with class title-product now How can I get from received html data-product-id but with my number 002212 ? 我收到的每a与类元素title-product现在如何从接收到的HTML获取data-product-id ,但与我的电话号码002212

I can't parse links to String. 我无法解析到String的links

I also tried something like this: 我也尝试过这样的事情:

if(links.contains("data-product-id=\"002212\"")){
                        System.out.println("it works");
                    } else {
                        System.out.println("nothing");
                    }

But links.contain s equals always " false " even this number is there. 但是,即使存在该数字, links.contain s也始终等于“ false ”。

also I tried 我也尝试过

it works but I get only first element with for example number 002211 instead of 002212 它可以工作,但是我只得到第一个元素,例如编号002211而不是002212

String linktext = a.attr("data-product-id");

and this is null 这是空的

String linktext = a.attr("data-product-id=\"002212\"");

解决了以下这一行。

Elements links = document.select("a[data-product-id=\"002212\"]");

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

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