简体   繁体   中英

How to get certain values from string

I am using proxyscrape to get free proxys but the current ouput is:

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

How can I slice up this string so I end up with an output which just is IP:PORT?

This is how my current code looks:

from proxyscrape import create_collector

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

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

print (proxygrab)

Any help would be very appreciated

This returns a collections.namedtuple , so you can just do:

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}")

Gives:

181.214.23.124:3129

proxygrab is not a string but an object. You could just print elements:

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

and to combine them:

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

would give:

181.214.23.210:3129
from proxyscrape import create_collector

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

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


# This print only the first element 181.214.23.210
print(proxygrab[0])

# This print only the first element 3129
print(proxygrab[1])

#so on for others

if you want only IP:PORT

# This print IP:PORT
print(proxygrab[0] + ":" + proxygrab[1])

if you want more then one IP:PORT (in this case is 10 ip:port)

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

# Change the second range number for print more or less ip:port
for index in range(1,10):

    proxygrab = collector.get_proxy({'code': 'us'})
    print(proxygrab[0] + ":" + proxygrab[1])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM