从 https://uzmanpara.milliyet.com.tr/doviz-kurlari/ 抓取货币兑换数据
Scrape currency exchange data from https://uzmanpara.milliyet.com.tr/doviz-kurlari/
我需要从网站获取货币数据,这里是网站 HTML
table 代码:
<tr>
<td class="currency-up"></td>
<td class="currency">
<a href="/dolar-kuru/" target="_blank" data-adservice-interactive-adunit="9927946/milliyet/uzmanpara/interstitial_oop">ABD Doları</a>
</td>
<td class>8,2805</td>
<td class>8,2856</td>
</tr>
我写了这些代码,但我无法处理这些代码:
String url = "https://uzmanpara.milliyet.com.tr/doviz-kurlari/";
Document doc = null;
try {
doc = Jsoup.connect(url).timeout(6000).get();
} catch (IOException ex) {
Logger.getLogger(den3.class.getName()).log(Level.SEVERE, null, ex);
}
Element link = doc.select("href").first();
String linkHref = link.attr("href"); // "http://example.com/"
System.out.println(linkHref);
但是我遇到了这个问题:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException:
Cannot invoke "org.jsoup.nodes.Element.attr(String)" because "link" is
null
如何处理这个问题,如何获取汇率。
你可以这样试试:
Element link = doc.select("a[href]").first();
如果您只键入 href
,它会搜索 href
标记名,但永远不会有这样的标记名。您必须寻找 a
标签的 href
属性。
让我们从一个简单的例子开始。
例如,要获取 href
值为 /dolar-kuru/
的元素下方的第 2 个 span
的值,您可以尝试:
// Example of selection with id.
Element element2 = doc.select("#usd_header_son_data").first();
String usd2 = element2.text();
System.out.println(usd2);
// Example of selecting 2nd span with href value and below. (1)
Element element1 = doc.select("a[href='/dolar-kuru/'] > span > span").first();
String usd1 = element1.text();
System.out.println(usd1);
// Example of selecting 2nd span with href value and below. (2)
Element element3 = doc.select("a[href='/dolar-kuru/'] > span :nth-child(2)").first();
String usd3 = element3.text();
System.out.println(usd3);
我们可以将示例更进一步。
让我们从 table 汇率中获取买入价和卖出价。
Elements elements = doc.select(".borsaMain > div:nth-child(2) > div:nth-child(1) > table td.currency");
for (Element element : elements) {
Elements curreny = element.parent().select("td:nth-child(2)");
Elements buy = element.parent().select("td:nth-child(3)");
Elements sell = element.parent().select("td:nth-child(4)");
System.out.println(String.format("%s [buy=%s, sell=%s]",
curreny.text(), buy.text(), sell.text()));
}
将给出如下所示的输出:
ABD Doları [buy=8,2855, sell=8,2888]
Euro [buy=9,8389, sell=9,8645]
İngiliz Sterlini [buy=11,4203, sell=11,4775]
Kanada Doları [buy=6,5696, sell=6,6091]
İsviçre Frangı [buy=9,0128, sell=9,0671]
Suudi Riyali [buy=2,2025, sell=2,2135]
...
可以使用更多不同的选择器,请参阅。 https://jsoup.org/cookbook/extracting-data/selector-syntax
对于提供的 HTML 代码,您可以执行以下操作:
Element link = doc.select("a[href]").first();
String linkHref = link.attr("href");
System.out.println(linkHref);
对于代码示例中提供的url,如果你只想select第一个,你可以这样做:
Element link = doc.select("td.currency > a").first();
String linkHref = link.attr("href");
System.out.println(linkHref);
只是为了解释前面的代码:“td.currency”将搜索带有 class“货币”的“td”标签。而“> a”将检索作为“a”标签的子元素。
如果你想要所有的货币,你可以做到
Elements links = doc.select("td.currency > a");
links.forEach(link -> System.out.println(link.attr("href")));
请注意,最后一个代码示例中有一些重复项。
我需要从网站获取货币数据,这里是网站 HTML
table 代码:
<tr>
<td class="currency-up"></td>
<td class="currency">
<a href="/dolar-kuru/" target="_blank" data-adservice-interactive-adunit="9927946/milliyet/uzmanpara/interstitial_oop">ABD Doları</a>
</td>
<td class>8,2805</td>
<td class>8,2856</td>
</tr>
我写了这些代码,但我无法处理这些代码:
String url = "https://uzmanpara.milliyet.com.tr/doviz-kurlari/";
Document doc = null;
try {
doc = Jsoup.connect(url).timeout(6000).get();
} catch (IOException ex) {
Logger.getLogger(den3.class.getName()).log(Level.SEVERE, null, ex);
}
Element link = doc.select("href").first();
String linkHref = link.attr("href"); // "http://example.com/"
System.out.println(linkHref);
但是我遇到了这个问题:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "org.jsoup.nodes.Element.attr(String)" because "link" is null
如何处理这个问题,如何获取汇率。
你可以这样试试:
Element link = doc.select("a[href]").first();
如果您只键入 href
,它会搜索 href
标记名,但永远不会有这样的标记名。您必须寻找 a
标签的 href
属性。
让我们从一个简单的例子开始。
例如,要获取 href
值为 /dolar-kuru/
的元素下方的第 2 个 span
的值,您可以尝试:
// Example of selection with id.
Element element2 = doc.select("#usd_header_son_data").first();
String usd2 = element2.text();
System.out.println(usd2);
// Example of selecting 2nd span with href value and below. (1)
Element element1 = doc.select("a[href='/dolar-kuru/'] > span > span").first();
String usd1 = element1.text();
System.out.println(usd1);
// Example of selecting 2nd span with href value and below. (2)
Element element3 = doc.select("a[href='/dolar-kuru/'] > span :nth-child(2)").first();
String usd3 = element3.text();
System.out.println(usd3);
我们可以将示例更进一步。
让我们从 table 汇率中获取买入价和卖出价。
Elements elements = doc.select(".borsaMain > div:nth-child(2) > div:nth-child(1) > table td.currency");
for (Element element : elements) {
Elements curreny = element.parent().select("td:nth-child(2)");
Elements buy = element.parent().select("td:nth-child(3)");
Elements sell = element.parent().select("td:nth-child(4)");
System.out.println(String.format("%s [buy=%s, sell=%s]",
curreny.text(), buy.text(), sell.text()));
}
将给出如下所示的输出:
ABD Doları [buy=8,2855, sell=8,2888]
Euro [buy=9,8389, sell=9,8645]
İngiliz Sterlini [buy=11,4203, sell=11,4775]
Kanada Doları [buy=6,5696, sell=6,6091]
İsviçre Frangı [buy=9,0128, sell=9,0671]
Suudi Riyali [buy=2,2025, sell=2,2135]
...
可以使用更多不同的选择器,请参阅。 https://jsoup.org/cookbook/extracting-data/selector-syntax
对于提供的 HTML 代码,您可以执行以下操作:
Element link = doc.select("a[href]").first();
String linkHref = link.attr("href");
System.out.println(linkHref);
对于代码示例中提供的url,如果你只想select第一个,你可以这样做:
Element link = doc.select("td.currency > a").first();
String linkHref = link.attr("href");
System.out.println(linkHref);
只是为了解释前面的代码:“td.currency”将搜索带有 class“货币”的“td”标签。而“> a”将检索作为“a”标签的子元素。
如果你想要所有的货币,你可以做到
Elements links = doc.select("td.currency > a");
links.forEach(link -> System.out.println(link.attr("href")));
请注意,最后一个代码示例中有一些重复项。