0

我想解析使用 voila python 呈现的 URL 的所有参数和查询字符串:

https://flyingcars.org/john/voila/render/shared/users/john/lrn_url.ipynb?dossier=SA12345678&autorun=True&name=jose&dossier=SA123445#{whatever='no',other='UK'}

到目前为止,我有:

URL_BASE = os.environ.get('SCRIPT_NAME')
PARAMETERS = os.environ.get("QUERY_STRING")
print(f'{URL_BASE=}')
print(f'{PARAMETERS=}')

我会得到:

URL_BASE='flyingcars.org/john/voila/render/shared/users/john/lrn_url.ipynb'
PARAMETERS='dossier=SA12345678&autorun=True&name=john'

但我有两个问题:

a) 如何获取URL中#后面的其余参数

b) 是否有标准库以字典的形式获取查询

我尝试了什么:

我正在尝试以这种方式使用 cgi 模块:

variables  = cgi.FieldStorage()
variables2 = dict(cgi.FieldStorage())
ks = cgi.FieldStorage().keys()
print(f'{ks=}')
vs = cgi.FieldStorage().values() # error here
for key in cgi.FieldStorage().keys():
    print(key)
    print(fieldStorage[key].value) # error here
   
print(f'{variables=}')
print(f'{variables2=}')

这给了我 fieldStorage[key].value 的错误,并且将 cgi.FieldStorage() 转换为 dict() 无法正常工作。

这些给出错误:

print(fieldStorage[key].value)

和上一行

fieldStorage().values()

我可以破解字符串,但可能有一种直接的方法可以使用 cgi 获取 dict 中的变量。在 cgi 中,我无法获取 URL 中 # 符号之外的参数。

谢谢

4

0 回答 0