【问题标题】:How to get the text after a split when using a function?使用函数时如何在拆分后获取文本?
【发布时间】:2018-09-27 18:39:53
【问题描述】:

我正在尝试将 user:pass:host:port 代理拆分为 user:pass 和 host:port,我知道如何获取 user:pass 时使用 proxytest = proxy.split("@")[0]代理是 user:pass@localhost:8080,它返回 user:pass 但是我怎样才能得到 localhost:8080?如果可能的话,最好是一个非常简单的方法。代理在 .txt 文件中打开,并且可能会更改,因此我不知道确切的字符串。

【问题讨论】:

  • 只需使用 "user:pass@localhost:8080".split("@")[1] 或 localPath =proxy.split("@")[1]
  • 您似乎不太可能无法修改您当前必须访问第二部分的 sn-p。如果您对该行的理解不够好,我建议您使用结构化教程,例如docs.python.org/3/tutorial.
  • @toheedNiaz 这不是我想要的分割字符串
  • @jonrsharpe 这么简单的事情是不可能的,这似乎很愚蠢?
  • 你的意思是这不是你想要的?这就是你现在正在做的!当然这不是不可能的,事实上它是如此微不足道,以至于唯一的慈善假设是你没有正确解释这个问题。我建议你edit这个问题。

标签: python proxies


【解决方案1】:

在 Python 2 中,您可以使用 urlparse.urlsplit 函数来完成此操作,而无需任何实际解析。

from urlparse import *

x = "http://user:pass@localhost:8080"
parts = urlsplit(x)

print parts.username        #Prints 'user'
print parts.password        #Prints 'pass'
print parts.hostname        #Prints 'localhost'
print parts.port            #Prints '8080'

如果您正在寻找一些非常幼稚的东西,您可以在@ 上进行拆分。

x = "user:pass@localhost:8080"
userpass, hostport = x.split('@')
print userpass, hostport    #Prints 'user:pass localhost:8080'

【讨论】:

  • 为什么每次都要拆分?
【解决方案2】:

答案真的很简单,朋友帮了我

prox = "user:pass@host:port"
userpass = prox[0:prox.find("@")]
hostport = prox[prox.find("@")+1:]

【讨论】:

  • userpass, hostport = prox.split('@')
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-05
  • 2021-02-05
  • 2020-12-08
  • 2013-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多