当前位置: 动力学知识库 > 问答 > 编程问答 >

jquery - Parsing data from html based on a string

问题描述:

I need to extract a particular value from a html page -- the related html content is as below:

-html --

<table border=0 cellspacing='1' cellpadding='0' class="content-denote" width="965" >

<tr height=17><td align=left class="text21"><b>Sensex : 26326.60

[22.40]

<img src="../images/arow_green.jpg" width="14" height="12">

&nbsp;&nbsp;&nbsp; Nifty : 7970.05 [14.60]

<img src="../images/arow_green.jpg" width="14" height="12">

-- end of html --

The value I need to extract is the value that appears after "Nifty : " - in the above example '7970.05'.

I have the following code to extract this:

--code--

$('td.text21').each(function () {

status = true;

var price1 = $(this).text().substr(340,7);

--end of code --

however at times I have noticed that the place of the value is not 340 - it changes sometimes to 303 because the img src link changes depending on some values.

The value will always be after "Nifty : " - this never changes.

Is there a way where I can code to extract the 7 digit value that appears after "Nifty : "?

Any help will be appreciated.

网友答案:

Instead of taking the substring from 340, you can take it from the position after where "Nifty :" is, which you get by doing

$(this).text().indexOf("Nifty :") + 7; // 7 is the length of "Nifty :"

So your new line would be:

var price1 = $(this).text().substr($(this).text().indexOf("Nifty :") + 7, 7);
分享给朋友:
您可能感兴趣的文章:
随机阅读: