简体   繁体   English

如何从没有ID或Class的标签中获取文本

[英]How to get a text from a tag that do not have ID or Class

I want to extract "Movie" text from this snippet using JSOUP : 我想使用JSOUP从此片段中提取“电影”文本: 在此处输入图片说明

As you can notice, the second span tag does not have ID or class neither, besides the first span. 您会注意到,除了第一个跨度之外,第二个跨度标签也没有ID或类。 My question is how can I retrieve that text ? 我的问题是如何检索该文本?

Thank you. 谢谢。

<span>                                                             
</span><span><span class="contentTitle">
Program Type:</span>
<span style="font-size: 14px;">
Movie</span>
<br />
</span><span id="MainContent_trProgramCategories"><span class="contentTitle">
 Categories:</span>&nbsp; 
<span style="font-size: 14px;">Horror, Thriller
</span>

尝试这个

Element element = doc.select("#MainContent_trProgramCategories  .contentTitle").get(0).nextElementSibling();

You need to keep whittling down the data by playing with the select(...) method. 您需要通过使用select(...)方法来继续减少数据消耗。 For instance, simply doing: 例如,只需做:

Elements myEles = doc.select("div[id=MainContent_UpdatePanel2] td");
String text = myEles.text();

System.out.println(text);

Will get you most of the stuff you're likely interested in. 将为您提供您可能感兴趣的大部分内容。

You can use what "Hovercraft Full Of Eels" suggested. 您可以使用“气垫船充满鳗鱼”的建议。

For future use cases though, the easiest way to get the CSS path or XPath for an element is to use Firebug extension. 不过,对于将来的用例,获取元素的CSS路径或XPath的最简单方法是使用Firebug扩展。

Firebug扩展

You can click the "mouse pointer looking icon" next to the "bug looking image" and choose the element that you want to retrieve the value from the browser and then the next row's XPath/CSS text box will give you the path that you can use. 您可以单击“查找错误的图像”旁边的“查找鼠标指针的图标”,然后选择要从浏览器中检索值的元素,然后下一行的XPath / CSS文本框将为您提供路径采用。

Simply copy that text and paste it in the code 只需复制该文本并将其粘贴到代码中

doc.select("HERE PASTE THE XPATH/CSS PATH THAT YOU COPIED FROM FIREBUG").text();

If you are using chrome, 如果您使用的是chrome,

you can 您可以

  1. right-click on an element that you want to retrieve the text value from 右键单击要从中检索文本值的元素
  2. choose "Inspect Element" 选择“检查元素”
  3. right-click again on the highlighted element in the debugger 再次右键单击调试器中突出显示的元素
  4. choose "Copy XPath" 选择“复制XPath”

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

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