简体   繁体   English

使用Jsoup获取数字值

[英]Getting a number value with Jsoup

I am using java to simply track the changes in a stock from the website BNN and I have it all set up to access the correct value. 我正在使用Java来简单地跟踪来自BNN网站的库存变化,并且已经全部设置好了以访问正确的值。 However, instead of a number value I simply get {{quoteObj.openPrc | number:2 }} 但是,我只是获得{{quoteObj.openPrc | number:2 }} {{quoteObj.openPrc | number:2 }} . {{quoteObj.openPrc | number:2 }} How do I access the number value displayed on the website? 如何访问网站上显示的数字值?

我的代码和输出:

Also if you could help me to get the current value on this website that would be really helpful too. 另外,如果您可以帮助我在此网站上获得当前的价值,那也将非常有帮助。

// Edit // //编辑//

org.jsoup.nodes.Document doc = null;
                try {
                    doc = Jsoup.connect("https://www.bnn.ca/stock/ACB.TO").userAgent("Mozilla/5.0").timeout(30000).get();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                org.jsoup.nodes.Element content = doc.getElementById("content-container");
                org.jsoup.nodes.Element value = content.getElementsByClass("value").get(0);
                doc.select("div.value:contains(quoteObj.high)");
                        Pattern pattern = Pattern.compile("number:(\\d+)");
                        Matcher matcher = pattern.matcher(value.text());
                        if (matcher.find()) {
                            String number = matcher.group(1);
                            System.out.println(number);
                        }`

I updated my code but now the output is just 2, it is simply taking the number from {{quoteObj.high | 我更新了代码,但现在输出仅为2,它只是从{{quoteObj.high | number:2 }} I want the actual number value displayed on the site. number:2}}我要在网站上显示实际的数字值。

I have attached an image of the webpage and the html code attached to the specific value I am trying to get. 我已经附上了网页图片,并将html代码附加到了我要获取的特定值上。

Assuming you have checked the source code of the website already, If not here is one snippet which you are trying to get using JSOUP. 假设您已经检查了网站的源代码,如果没有,请尝试使用JSOUP获得一个摘要。

  <div ng-controller="stockDetailSummary" ng-cloak> <div class="stockDetail" ng-show="quoteFound"> <div ng-show="DataLoaded"> <h2>Summary</h2> <div class="Column Left"> <div class="row"> <div class="label"> Open </div> <div class="value"> {{quoteObj.openPrc | number:2 }} </div> </div> <div class="row alt"> <div class="label"> High </div> <div class="value"> {{quoteObj.high | number:2 }} </div> </div> <div class="row"> <div class="label"> Low </div> <div class="value"> {{quoteObj.low | number:2 }} </div> 

If you see 如果你看到
<div class="value"> {{quoteObj.openPrc | number:2 }} </div>
is returning what you are getting as output. 返回您得到的输出。 Take it as String and do what so ever modification you want to do. 将其作为String并进行您想做的修改。

For your information the website is built on angularjs and the {{quoteObj.openPrc | 供您参考,该网站建立在angularjs和{{quoteObj.openPrc | number:2 }} are Angular pipes, a way to write display-value transformations that you can declare in your HTML. number:2}}是Angular管道,它是一种编写可以在HTML中声明的显示值转换的方式。

EDIT: 编辑:

As per your attached image, The data you want to retrieve is 根据您的附件图像,要检索的数据是 在此处输入图片说明

If you go into network tab of developer console you will find the exact request which is fetching the data 如果进入开发者控制台的“网络”标签,您将找到正在获取数据的确切请求 在此处输入图片说明

And corresponding response also you can see by navigating to preview tab 您也可以通过导航到预览选项卡来查看相应的响应 在此处输入图片说明

The same has been reffered in the html source code which you can see by pressing CTRL+U 通过按CTRL + U可以看到html源代码中的相同内容 在此处输入图片说明

 <div class="stockDetail" ng-show="quoteFound"> <div ng-show="DataLoaded" class=""> <h2>Summary</h2> <div class="Column Left"> <div class="row"> <div class="label">Open</div> <div class="value ng-binding">0.00</div> </div> <div class="row alt"> <div class="label">High</div> <div class="value ng-binding">0.00</div> </div> <div class="row"> <div class="label">Low</div> <div class="value ng-binding">0.00</div> </div> <div class="row alt"> <div class="label">Volume</div> <div class="value ng-binding">0</div> </div> <div class="row"> <div class="label">52-Week High</div> <div class="value ng-binding">8.66</div> </div> <div class="row alt"> <div class="label">52-Week Low</div> <div class="value ng-binding">1.90</div> </div> </div> <div class="Column Right"> <div class="row"> <div class="label">Exchange</div> <div class="value ng-binding">TOR</div> </div> <div class="row alt"> <div class="label">Currency</div> <div class="value ng-binding">CAD</div> </div> <div class="row"> <div class="label">P/E</div> <div class="value ng-binding">0.00</div> </div> <div class="row alt"> <div class="label">EPS</div> <div class="value ng-binding">-0.01</div> </div> <div class="row"> <div class="label">Dividend</div> <div class="value ng-binding">0.00</div> </div> <div class="row alt"> <div class="label">Yield</div> <div class="value ng-binding">0.00<!-- ngIf: quoteObj.yield --></div> </div> </div> <div class="clear"></div> </div> </div> 

But here are few problems while fetching these values using JSOUP. 但是,在使用JSOUP获取这些值时,这里存在一些问题。

  1. JSOUP is a HTML Parser, It will not be able to fetch any content rendered by Javascript. JSOUP是HTML解析器,它将无法获取Javascript呈现的任何内容。
  2. The following Content is rendered by below api URL which is returning JSON data. 以下内容由返回JSON数据的api URL呈现。

Request URL:https://data.bnn.ca/dispenser/bnnApi/quote/summary?s=ACB.TO

 { "statusCode": 200, "generatedTimestamp": "2017-11-30T04:47:30.793-05:00", "duration": 61, "data": { "stocks": [{ "symbol": "ACB.TO", "currency": "CAD", "trdprc": 0.0, "volume": 0, "dividend": 0.0, "earnings": -0.01375, "high": 0.0, "low": 0.0, "openPrc": 0.0, "perRatio": 0.0, "tradeDate": "29 NOV 2017", "yield": 0.0, "yearHigh": 8.66, "yearLow": 1.9, "exchange": "TOR" }], "invalidSymbols": [] } } 

So it will be better if you go through these links 因此,如果您通过这些链接进行操作会更好
Is there a way to embed a browser in Java? 有没有办法在Java中嵌入浏览器?

https://stackoverflow.com/a/47485387/5313817 https://stackoverflow.com/a/47485387/5313817

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

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