Hendrik B.
Hendrik B.

Reputation: 85

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

Upvotes: 1

Views: 168

Answers (3)

Piero
Piero

Reputation: 646

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])

Upvotes: 0

seymourgoestohollywood
seymourgoestohollywood

Reputation: 1167

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

Upvotes: 3

Chris Maes
Chris Maes

Reputation: 37742

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

Upvotes: 2

Related Questions