如何从字符串中获取某些值

2024-09-29 21:36:41 发布

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

我正在使用proxyscrape获取免费proxy,但当前输出为:

Proxy(host='181.214.23.210', port='3129', code='us', country='united states', anonymous=True, type='https', source='us-proxy')

我怎样才能切分这个字符串,从而得到一个仅仅是IP:PORT的输出呢?你知道吗

我当前的代码是这样的:

from proxyscrape import create_collector

collector = create_collector('my-collector', 'http')

# Retrieve only 'us' proxies
proxygrab = collector.get_proxy({'code': 'us'})

print (proxygrab)

任何帮助都将不胜感激


Tags: truehostportcreatecodecountryunitedproxy
2条回答

proxygrab不是字符串而是对象。您可以只打印元素:

print(proxygrab.host)
print(proxygrab.port)

把它们结合起来:

print("{}:{}".format(proxygrab.host, proxygrab.port))

将给出:

181.214.23.210:3129

这将返回一个collections.namedtuple,因此您可以执行以下操作:

from proxyscrape import create_collector

collector = create_collector('my-collector', 'http')

# Retrieve only 'us' proxies
proxygrab = collector.get_proxy({'code': 'us'})

print(f"{proxygrab.host}:{proxygrab.port}")

提供:

181.214.23.124:3129

相关问题 更多 >

    热门问题