user8314924
user8314924

Reputation:

Cycle through values in an array

I am trying to cycle through values in an array in python, I dont know how to do this.

pid = {
    "021554",
    "098765",
    "287004",
    "237655"
}

Above is my array, im trying to use pid to put it in a link, for example:

get("www.google.com/" + pid)

I want it to use pid number 1 then number 2 then number 3 and so till it goes back to the top of the list and starts again in an else if.

if 'text' in response.text:
    do something
else:
    use next pid to add to the link and start script from the top again using the new id`

Upvotes: 0

Views: 1154

Answers (1)

Hugh Bothwell
Hugh Bothwell

Reputation: 56714

from itertools import cycle
import requests

for pid in cycle("021554 098765 287004 237655".split()):
    url = "www.google.com/{}".format(pid)
    txt = requests.get(url).text
    if "something" in txt:
        break

Do be aware that this could run forever if "something" doesn't show up.

Upvotes: 1

Related Questions