JSoup,如何 return 来自动态 <a href> 标签的数据
JSoup, how to return data from a dynamic <a href> tag
对 JSoup 非常陌生,试图检索存储在标签中的可变值,特别是来自以下 website 和 html。
Snapshot of HTML
“consitituency/”后的结果是可变的,取决于用户的输入。我能够自己检索 h2 标签,但不能检索其中的信息。目前我能得到的最好的 return 只是使用下面方法的标签
我可以将所需的 return 子字符串化为
Dublin Bay South
实际return是
<well.col-md-4.h2></well.col-md-4.h2>
private String jSoupTDRequest(String aLine1, String aLine3) throws IOException {
String constit = "";
String h2 = "h2";
String url = "https://www.whoismytd.com/search?utf8=✓&form-input="+aLine1+"%2C+"+aLine3+"+Ireland";
//Switch to try catch if time
Document doc = Jsoup.connect(url)
.timeout(6000).get();
//Scrape elements from relevant section
Elements body = doc.select("well.col-md-4.h2");
Element e = new Element("well.col-md-4.h2");
constit = e.toString();
return constit;
我对 JSoup 和一般的抓取都非常陌生。非常感谢知道自己在做什么的人的任何意见或尝试获得所需结果的任何替代方法
从相关部分代码中更改您的抓取元素,如下所示:
Select 首先是第一个 <div class="well">
元素。
Element tdsDiv = doc.select("div.well").first();
Select 接下来是第一个 <a>
link 元素。这个link指向选区
Element constLink = tdsDiv.select("a").first();
抓取本link的文字内容获取选区名称
constit = constLink.text();
import org.junit.jupiter.api.Test;
import java.io.IOException;
@DisplayName("JSoup, how to return data from a dynamic <a href> tag")
class JsoupQuestionTest {
private static final String URL = "https://www.whoismytd.com/search?utf8=%E2%9C%93&form-input=Kildare%20Street%2C%20Dublin%2C%20Ireland";
@Test
void findSomeText() throws IOException {
String expected = "Dublin Bay South";
Document document = Jsoup.connect(URL).get();
String actual = document.getElementsByAttributeValue("href", "/constituency/dublin-bay-south").text();
Assertions.assertEquals(expected, actual);
}
}
对 JSoup 非常陌生,试图检索存储在标签中的可变值,特别是来自以下 website 和 html。 Snapshot of HTML
“consitituency/”后的结果是可变的,取决于用户的输入。我能够自己检索 h2 标签,但不能检索其中的信息。目前我能得到的最好的 return 只是使用下面方法的标签
我可以将所需的 return 子字符串化为
Dublin Bay South
实际return是
<well.col-md-4.h2></well.col-md-4.h2>
private String jSoupTDRequest(String aLine1, String aLine3) throws IOException {
String constit = "";
String h2 = "h2";
String url = "https://www.whoismytd.com/search?utf8=✓&form-input="+aLine1+"%2C+"+aLine3+"+Ireland";
//Switch to try catch if time
Document doc = Jsoup.connect(url)
.timeout(6000).get();
//Scrape elements from relevant section
Elements body = doc.select("well.col-md-4.h2");
Element e = new Element("well.col-md-4.h2");
constit = e.toString();
return constit;
我对 JSoup 和一般的抓取都非常陌生。非常感谢知道自己在做什么的人的任何意见或尝试获得所需结果的任何替代方法
从相关部分代码中更改您的抓取元素,如下所示:
Select 首先是第一个
<div class="well">
元素。Element tdsDiv = doc.select("div.well").first();
Select 接下来是第一个
<a>
link 元素。这个link指向选区Element constLink = tdsDiv.select("a").first();
抓取本link的文字内容获取选区名称
constit = constLink.text();
import org.junit.jupiter.api.Test;
import java.io.IOException;
@DisplayName("JSoup, how to return data from a dynamic <a href> tag")
class JsoupQuestionTest {
private static final String URL = "https://www.whoismytd.com/search?utf8=%E2%9C%93&form-input=Kildare%20Street%2C%20Dublin%2C%20Ireland";
@Test
void findSomeText() throws IOException {
String expected = "Dublin Bay South";
Document document = Jsoup.connect(URL).get();
String actual = document.getElementsByAttributeValue("href", "/constituency/dublin-bay-south").text();
Assertions.assertEquals(expected, actual);
}
}