[英]Jsoup select elements from dropdown menu
I have been trying to access a specific element from a dropdown menu on a Steam leaderboard page.我一直在尝试从 Steam 排行榜页面的下拉菜单中访问特定元素。 Is there a good way to access a specific 'option value'?是否有访问特定“选项值”的好方法? I have tried the following code and it isn't grabbing any value (debugger shows levelName as "").我尝试了以下代码,但它没有获取任何值(调试器将 levelName 显示为“”)。
String url = "http://steamcommunity.com/stats/592300/leaderboards/1900835";
Document document = Jsoup.connect(url).get();
String levelName = document.select("div#leaderHeader > option:contains(1900822)").val();
System.out.println(levelName);
There are few mistakes in your code:您的代码中几乎没有错误:
:contains
doesn't check attributes, but text generated by element. :contains
不检查属性,而是检查元素生成的文本。 If you want to find <option value="1900822">someText</option>
using value="1900822"
then you can use [attr=value]
selector like option[value=1900822]
(without quotes)如果您想使用value="1900822"
查找<option value="1900822">someText</option>
那么您可以使用[attr=value]
选择器,如option[value=1900822]
(不带引号)parent > child
but <option ...>
is child of <select ...>
element, not <div id="leaderHeader">
.您使用了parent > child
但<option ...>
是<select ...>
元素的 child,而不是<div id="leaderHeader">
。 This div is its ancestor, but it is not parent, so remove that >
这个 div 是它的祖先,但它不是父级,所以删除它>
val()
method here, but I am guessing you want to use text()
.我不确定您在这里尝试使用val()
方法做什么,但我猜您想使用text()
。So you probably want:所以你可能想要:
String levelName = document.select("div#leaderHeader option[value=1900822]").text();
Just notice that there are some non-breaking spaces
请注意,有一些不间断的空格
in result.结果。 You may want to replace them with simple spaces and after that trim leading and trailing ones.您可能想用简单的空格替换它们,然后修剪前导和尾随的空格。
levelName = levelName.replace((char)160,' ').trim();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.