0

I have a python script which searches a web page for information. currently I add the search term as a parameter when i run my program 'myscript.py searchterm'.

What I would like to do is to have a file with my search terms in, get my script to loop through each one in turn on its own.

so I would populate my list from a file like this....

with open('mylist.txt', 'r') as f:
    searchterms = f.readlines()

I already have my code which looks something like this...just to give you an idea of layout...

counter = 0
try:
    while counter <10:

    #do some other stuff here

    counter=counter+10

except IOError:
    print "No result found!"+""

I need to wrap this in another loop to do this for every item in my list and I'm failing.

I know I need to reset the counter if it gets to 10, move onto my next list item and loop through the above but I don't know how. I find the python docs difficult to understand and I would appreciate a little help.

TIA

3
  • This is very unclear. What's the point of the counter? Why do you need to increase it by 10 each time? Commented Jan 31, 2013 at 16:00
  • because it gets 10 results every time the web page is searched which is all i need if i posted the entire code you would understand but i'm not willing to do that at the moment. Commented Jan 31, 2013 at 16:02
  • I just need to perform the above code for each one of my list items. Commented Jan 31, 2013 at 16:03

1 Answer 1

1

If you have a list of search terms, you can easily loop through them and pass them to your existing loop like:

for searchterm in searchterms:   
    counter = 0
    try:
        while counter <10:

        #do some other stuff here with searchterm

        counter=counter+10

    except IOError:
        print "No result found!"+""

Be sure that your counter correctly resets at the beginning of each search term's loop.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.