使用Jsoup获取数值

时间:2017-11-29 00:39:35

标签: java html jsoup

我正在使用java来简单地跟踪网站BNN中股票的变化,并将其全部设置为访问正确的值。但是,我只需获得{{quoteObj.openPrc | number:2 }}而不是数字值。如何访问网站上显示的数字值?

My code and the output:

另外,如果你能帮助我获得这个网站上的当前价值,那将非常有帮助。

//编辑//

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);
                        }`

我更新了我的代码,但现在输出只是2,它只是从{{quoteObj.high | number:2}}我想要在网站上显示的实际数字值。

I have attached an image of the webpage and the html code attached to the specific value I am trying to get.

1 个答案:

答案 0 :(得分:0)

假设您已经检查了网站的源代码,如果不是这里是您尝试使用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> 
&#13;
&#13;
&#13;

如果您看到<div class="value"> {{quoteObj.openPrc | number:2 }} </div>
正在返回你得到的输出。把它作为字符串,做你想做的修改。

为了您的信息,该网站建立在angularjs和{{quoteObj.openPrc | number:2}}是Angular管道,一种编写可以在HTML中声明的显示值转换的方法。

修改

根据您附加的图片,您要检索的数据是 enter image description here

如果您进入开发者控制台的网络选项卡,您将找到获取数据的确切请求 enter image description here

通过导航到预览标签,您也可以看到相应的响应 enter image description here

通过按CTRL + U可以看到html源代码中的相同内容 enter image description here

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

但是使用JSOUP获取这些值时出现的问题很少。

  1. JSOUP是一个HTML Parser,它无法获取Javascript呈现的任何内容。
  2. 以下内容由下面的api URL呈现,该URL返回JSON数据。
  3. Request URL:https://data.bnn.ca/dispenser/bnnApi/quote/summary?s=ACB.TO

    &#13;
    &#13;
    {
      "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": []
      }
    }
    &#13;
    &#13;
    &#13;

    因此,如果您浏览这些链接会更好Is there a way to embed a browser in Java?

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