使用urllib更改时间参数

问题描述 投票:1回答:1

我需要更改to和from参数,但是urllib错误地收集了所有内容。在字典中,参数正确更改,但是随后收集了旧的url。

import urllib.parse
url = 'http://192.168.150.138:3000/render/d-solo/htddR-gGz/jmeter-dashboard?orgId=1&from=1589972154000&to=1589972739000&var-data_source=InfluxDB-Jmeter&var-application=application%20name&var-transaction=HTTP%20Request%20-%20homepage&var-measurement_name=jmeter&var-send_interval=5&panelId=23&width=1600&height=500&tz=Europe%2FMoscow'

urlStr: urllib.parse.ParseResult = urllib.parse.urlparse(url)
urlDict = urllib.parse.parse_qs(urlStr.query)
print(urlDict)
urlDict['from'] = ['1555926954000']
urlDict['to'] = ['1555938339000']
print(urlDict)
urlStr._replace(query=urllib.parse.urlencode(urlDict))
print(urlStr.geturl())

[Output]:
{'orgId': ['1'], 'from': ['1589972154000'], 'to': ['1589972739000'], 'var-data_source': ['InfluxDB-Jmeter'], 'var-application': ['application name'], 'var-transaction': ['HTTP Request - homepage'], 'var-measurement_name': ['jmeter'], 'var-send_interval': ['5'], 'panelId': ['23'], 'width': ['1600'], 'height': ['500'], 'tz': ['Europe/Moscow']}
{'orgId': ['1'], 'from': ['1555926954000'], 'to': ['1555938339000'], 'var-data_source': ['InfluxDB-Jmeter'], 'var-application': ['application name'], 'var-transaction': ['HTTP Request - homepage'], 'var-measurement_name': ['jmeter'], 'var-send_interval': ['5'], 'panelId': ['23'], 'width': ['1600'], 'height': ['500'], 'tz': ['Europe/Moscow']}
http://192.168.150.138:3000/render/d-solo/htddR-gGz/jmeter-dashboard?orgId=1&from=1589972154000&to=1589972739000&var-data_source=InfluxDB-Jmeter&var-application=application%20name&var-transaction=HTTP%20Request%20-%20homepage&var-measurement_name=jmeter&var-send_interval=5&panelId=23&width=1600&height=500&tz=Europe%2FMoscow

python python-3.x url urllib
1个回答
2
投票

我认为_replace不执行就地替换。尝试将返回值分配回urlStr

urlStr=urlStr._replace(query=urllib.parse.urlencode(urlDict, doseq=True))
© www.soinside.com 2019 - 2024. All rights reserved.