从Web URL中提取值

时间:2017-11-27 10:19:30

标签: python

我正尝试使用以下代码从URL中提取值:

import requests
import lxml.html as LH

url = "https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=PFC&instrument=FUTSTK"

html = requests.get(url).content
tree = LH.fromstring(html)
node = tree.xpath("//*[@id='annualisedVolatility']")[0]
val = node.text
print(val)

...期待以下价值:

<span id="annualisedVolatility">38.98</span>

希望了解错误的位置。

1 个答案:

答案 0 :(得分:0)

不知道为什么我有一个downvote!弄清楚了。这是代码:

import requests
import json

url = "https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=PFC&instrument=FUTSTK"
html = requests.get(url).text
data=soup.find(id="responseDiv").text.strip()

d1 = json.loads(data)
d2=float(d1['data'][0]['annualisedVolatility'])
print(d2)