[英]error in using getelementbytag() to fetch data from a html in jsoup
Hi I am trying to get data using crawling from html given below I got the entire html by following嗨,我正在尝试使用从下面给出的 html 中抓取来获取数据,我通过以下方式获得了整个 html
Elements b= doc.getElementsByClass("a");
Then for getting the value from tr tag I used然后为了从我使用的 tr 标签中获取值
Elements tr = b.getElementsByTag("tr");
But it shows the error as T he method getElementsByTag(String) is undefined for the type Elements但它显示错误,因为该方法 getElementsByTag(String) 未定义为元素类型
I want to get the values for tr tag please help me in it.我想获取 tr 标签的值,请帮助我。 The html which i am working is given below,
下面给出了我正在工作的 html,
<table class="a"> <thead> <tr> <td>the<br>boy</td> <td>ran<br>well</td> <td>the<br>boy</td> <td>ran<br>well</td> </tr> </thead> <tbody> <tr> <td>6</td> <td>1</td> <td>1</td> <td>1</td> </tr>
It's not clear what we are talking about, java or javascript.目前还不清楚我们在谈论什么,java 还是 javascript。
But instead of fetching first the elements with className a
and then iterating over the result to fetch the elements with tagname tr
you may fetch the <tr/>
's with a single statement.但是,不是首先获取带有 className
a
的元素,然后迭代结果以获取带有标记名tr
的元素,您可以使用单个语句获取<tr/>
。
In javascript the method would be querySelectorAll
:在 javascript 中,方法是
querySelectorAll
:
var tr = document.querySelectorAll(".a tr");
In jsoup(I'm not familiar with java/jsoup, so that's just a guess) exists a method select
在jsoup(我不熟悉java/jsoup,所以这只是一个猜测)中存在一个方法
select
Elements tr = doc.select(".a tr");
To get html elements using class and tag names使用类和标签名称获取 html 元素
Use below code.使用下面的代码。
var a= document.getElementsByClassName("a");
var b= document.getElementsByTagName("tr");
It should work :-)它应该工作:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.