Java Jsoup从div类中随机选择一个(1)
在页面中,有多个同一类的div,如下所示:Java Jsoup从div类中随机选择一个(1),java,jsoup,Java,Jsoup,在页面中,有多个同一类的div,如下所示: <div class="author-quote"> <a href="#">Maldives</a> </div> 但我只想要一个,一个随机的。我该怎么做 其他信息:一些标签包含多个单词的术语,如爱尔兰共和国和几内亚比绍,一些标签具有多米尼加共和国等符号 更新:我可以使用一些字符串操作来分离它们。但是我希望我可以使用Jsoup的元素选择工具来实现这一点。您可以拆分值。通过分隔符“”获取(0)
<div class="author-quote">
<a href="#">Maldives</a>
</div>
但我只想要一个,一个随机的。我该怎么做
其他信息:一些
标签包含多个单词的术语,如爱尔兰共和国和几内亚比绍,一些标签具有多米尼加共和国等符号
更新:我可以使用一些字符串操作来分离它们。但是我希望我可以使用Jsoup的元素选择工具来实现这一点。您可以拆分
值。通过分隔符“”
获取(0)
(从而生成一个字符串数组),然后使用索引到数组中,并选择一个随机字符串
对象
...
...
...
String[] tempArray = value.get(0).split(" ");
return tempArray[new Random().nextInt(tempArray.length)];
更新
根据您的后期更新,另一个替代解决方案是从网页端,您可以在一个主div
元素(如果尚未完成)中包围所有具有class
属性author quote
的div,然后选择此父div
,这样,您就可以在parentDiv
ChildNodes上进行迭代,分别收集它们的文本,然后添加到ArrayList
private String get() throws InterruptedException{
final CountDownLatch latch = new CountDownLatch(1);
final List<String> value = new ArrayList<>();
Thread thread = new Thread(new Runnable() {
Element parentDiv;
@Override
public void run() {
try {
Document doc = Jsoup.connect(WEB_URL).get();
parentDiv = //getTheParentDivSomehow()
for (Node child : parentDiv.childNodes()) {
Node tempNode = child.childNodes().get(0);
if (tempNode.childNodes().get(0) instanceof TextNode)
value.add(((TextNode) child).text());
}
latch.countDown();
} catch (IOException e) {
Log.e(TAG,e.getMessage());
}
}// end run
});
thread.start();
latch.await();
Collections.shuffle(value);
return value.get(new Random().nextInt(value.size()));
}
private String get()抛出InterruptedException{
最终倒计时闩锁=新倒计时闩锁(1);
最终列表值=新的ArrayList();
Thread Thread=新线程(new Runnable(){
元素parentDiv;
@凌驾
公开募捐{
试一试{
Document doc=Jsoup.connect(WEB_URL.get();
parentDiv=//getTheParentDivSomehow()
对于(节点子节点:parentDiv.childNodes()){
节点tempNode=child.childNodes().get(0);
if(tempNode.childNodes().get(0)instanceof TextNode)
value.add(((TextNode)child.text());
}
倒计时();
}捕获(IOE异常){
Log.e(标记,e.getMessage());
}
}//终点
});
thread.start();
satch.wait();
集合。洗牌(值);
返回value.get(newrandom().nextInt(value.size());
}
私有字符串get()抛出InterruptedException{
最终倒计时闩锁=新倒计时闩锁(1);
最终列表值=新的ArrayList();
Thread Thread=新线程(new Runnable(){
元素;
@凌驾
公开募捐{
试一试{
Document doc=Jsoup.connect(WEB_URL.get();
//返回元素列表
元素=文件选择(“div.author-quote”);
//使用随机数并获取该索引处的元素以随机选择一个div元素
随机=新随机();
int randomIndex=random.nextInt(elements.size());//返回介于0和elements.size()之间的随机数
value.add(elements.get(randomIndex.text());//只添加随机元素的文本
倒计时();
}捕获(IOE异常){
Log.e(标记,e.getMessage());
}
}//终点
});
thread.start();
satch.wait();
返回值.get(0);
}
我假设整个输出存储为值中的第一个元素
数组列表?这是对的吗?@Aominè是的。我这样做是因为要从线程中获取值,必须声明最终的数据结构。整个输出都存储在value中。get(0)很抱歉没有透露这一点,但是一些
标记包含多个单词,如爱尔兰共和国和几内亚比绍,还有一些带有符号,如多米尼加共和国。我们可以在Jsoup元素
选择中做些什么吗?@DylanCzenski我以前没有使用过Jsoup,但经过一些研究后,我得到了一个更新的答案,如果它不起作用,请告诉我。
...
...
...
String[] tempArray = value.get(0).split(" ");
return tempArray[new Random().nextInt(tempArray.length)];
private String get() throws InterruptedException{
final CountDownLatch latch = new CountDownLatch(1);
final List<String> value = new ArrayList<>();
Thread thread = new Thread(new Runnable() {
Element parentDiv;
@Override
public void run() {
try {
Document doc = Jsoup.connect(WEB_URL).get();
parentDiv = //getTheParentDivSomehow()
for (Node child : parentDiv.childNodes()) {
Node tempNode = child.childNodes().get(0);
if (tempNode.childNodes().get(0) instanceof TextNode)
value.add(((TextNode) child).text());
}
latch.countDown();
} catch (IOException e) {
Log.e(TAG,e.getMessage());
}
}// end run
});
thread.start();
latch.await();
Collections.shuffle(value);
return value.get(new Random().nextInt(value.size()));
}
private String get() throws InterruptedException{
final CountDownLatch latch = new CountDownLatch(1);
final List<String> value = new ArrayList<>();
Thread thread = new Thread(new Runnable() {
Elements elements;
@Override
public void run() {
try {
Document doc = Jsoup.connect(WEB_URL).get();
//returns a list of Elements
elements = doc.select("div.author-quote");
//use a random number and get the Element at that index to select one div element at random
Random random = new Random();
int randomIndex = random.nextInt(elements.size());//returns a random number between 0 and elements.size()
value.add(elements.get(randomIndex).text()); // add only the text of the random Element
latch.countDown();
} catch (IOException e) {
Log.e(TAG,e.getMessage());
}
}// end run
});
thread.start();
latch.await();
return value.get(0);
}