Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中执行代码时,如何更改URL中的scrip名称_Python - Fatal编程技术网

在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)
    ...