在python中替换url的一部分

2024-10-01 13:28:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在selenium webdriver+python中替换以下url的一部分:

https://ve-215:8443/cloudweb/dropbox_authorized?oauth_token=l8eYuFG8nux3TUHm&uid=69768040

我需要将ve-215替换为ip地址,比如192.168.24.53

我尝试过使用replace,但它不起作用。在

以下是我使用的代码:

current_url=driver.current_url
print(current_url) #prints the url of the current window.

current_url.replace("ve-215", "192.168.53.116")
print(current_url)  #print url with replaced string
driver.get(current_url) #open window with replaced url

有谁能帮我解决上面代码的问题吗?在


Tags: the代码httpsurldriverseleniumwithve
2条回答

replace方法返回一个应用了修改的字符串,但不修改当前字符串。在

你应该这样使用它:

current_url = driver.current_url
print(current_url) #prints the url of the current window.

current_url = current_url.replace("ve-215", "192.168.53.116")
print(current_url)  #print url with replaced string
driver.get(current_url) #open window with replaced url

replace方法不修改字符串本身(字符串在Python中是不可变的),但返回一个新的字符串。试试看

current_url = current_url.replace("ve-215", "192.168.53.116")

尽管如此,建议使用^{}模块(python3中的^{})来解析和重构url。在

相关问题 更多 >