在python中执行代码时,如何更改URL中的scrip名称
我有在python中执行代码时,如何更改URL中的scrip名称,python,Python,我有python代码,它为派生脚本提供了VWAP值 import requests import json from bs4 import BeautifulSoup as bs r = requests.get('https://nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=INFY&instrument=FUTSTK&expiry=30MAY2019&a
python
代码,它为派生脚本提供了VWAP
值
import requests
import json
from bs4 import BeautifulSoup as bs
r = requests.get('https://nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=INFY&instrument=FUTSTK&expiry=30MAY2019&type=-&strike=-')
soup = bs(r.content, 'lxml')
data = json.loads(soup.select_one('#responseDiv').text.strip())
vwap = data['data'][0]['vwap']
print(vwap)
URL有一种模式,其中只有基础更改的名称。
例如,在给定的2个URL中:
https://nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=RELIANCE&instrument=FUTSTK&expiry=30MAY2019&type=-&strike=-
https://nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=INFY&instrument=FUTSTK&expiry=30MAY2019&type=-&strike=-
当URL中的脚本名称和脚本名称更改时,程序请求输入的代码可能是什么?请求的参数。
get(…)
是一个字符串,您可以像字符串一样操作它。对于这种情况,我建议使用(或者您也可以使用f-strings
)
如果URL的其他部分在每次调用时需要不同,这种方法还允许您使用更多参数;您只需向
.format()
调用添加更多参数(并相应地修改base\u url
以接受这些新参数)。请求的参数。get(…)
是一个字符串,您可以像处理字符串一样处理它。对于这种情况,我建议使用(或者您也可以使用f-strings
)
如果URL的其他部分在每次调用时需要不同,这种方法还允许您使用更多参数;您只需向
.format()
调用添加更多参数(并相应地修改base\u url
以接受这些新参数)。url是字符串,因此您可以使用任何字符串函数,即“底层={}…”.format(“其他文本”)
。您可以更改url中参数的顺序,并放置基础=
和结束并连接值”https://...&underlying=“+”其他_文本“
url是字符串,因此您可以使用任何字符串函数-即“底层={}…”格式(“其他_文本”)
。您可以更改url中参数的顺序,并放置基础=
和结束并连接值”https://...&underlying=“+”其他文字“
非常感谢@RalfThank非常感谢@Ralf
base_url = 'https://nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying={}&instrument=FUTSTK&expiry=30MAY2019&type=-&strike=-'
underlying_list = ['RELIANCE', 'INFY']
for underlying in underlying_list:
url = base_url.format(underlying)
print(url)
resp = requests.get(url)
...