0

I'm fairly new to Python, so I'm hoping someone can help me generate a list of unique URLs based on information in a text file.

Example: I have the base URL, www.website.com/users/, and a txt file with usernames, 'frank', 'rachel', 'james', etc. And I want to create URLs with this information, and save it to a txt file, like this:

www.website.com/users/frank
www.website.com/users/rachel
www.website.com/users/james
etc.

I have done something similar with numbers, e.g.

www.website.com/1
www.website.com/2
etc.

The code I wrote for the number solution is pasted below in case it's helpful as a starting point.

import time

htmlTxt=""
pageNum=0
x="http://forum.com/eforum/forumdisplay.php?fid=13&page="
y=x+str(pageNum)

file = open("URLs.txt", "wb")
while True:
    try:
        time.sleep(0.001)  # do something here
        file.write(x +str(pageNum)+"\n")
    pageNum+=1

    except KeyboardInterrupt:
        print '\nPausing...  (Hit ENTER to continue, type quit to exit.)'
        try:
            response = raw_input()
            if response == 'quit':
                break
            print 'Resuming...'
        except KeyboardInterrupt:
            print 'Resuming...'
            continue
file.close()

(the reason I used 'time' in the example above is because I don't know how to make it stop at a certain number, so I just let it ran for a few seconds and deleted the URLs that went beyond the 'max' number.)

Thanks in advance!

1
  • Can you simplify your question? Is www.website.com the same site every time in the expected output? Where does it come from? What does the list of names look like? One per line in a file? Commented Apr 2, 2014 at 18:53

4 Answers 4

1

Open the file that contains names for reading, another one that will contain the output - for writing. Read the input file line by line and write to the output appending the name:

URL = "www.website.com/users/"

with open('input.txt', 'r') as input_file:
    with open('output.txt', 'w') as output_file:
        for line in input_file:
            output_file.write(URL + line)

For the input.txt that contains:

frank
rachel
james

it produces the following output.txt:

www.website.com/users/frank
www.website.com/users/rachel
www.website.com/users/james
Sign up to request clarification or add additional context in comments.

Comments

1
  • Open the file with the usernames to read and output file to write.
  • Read a line from the username file, construct the url and write it to output file.

    with open('usernames', 'r') as input_file, open('output', 'a') as output_file:
        for line in input_file:
            url = "http://website.com/{}".format(line.strip())
            output_file.write(url)
    input_file.close()
    output_file.close()
    

3 Comments

is .format better (more pythonic?) than just concatenating with +?
@Jasper The documentation says it is a newer way of formatting strings. I just prefer it for readability reasons.
OK, agreed in general, but I think I'd use + instead of {} if it's at the end of the string.
1

On "how to make it stop at a certain number": You can use a for loop, which is usually used to iterate over a list:

for i in range(maxnumber):
    # this body is executed maxnumber times and i is 0, 1, ..., maxnumber - 1

Comments

0

This is working fine.

URL = "www.website.com/users/"

with open('input.txt', 'r') as input_file:
    with open('output.txt', 'w') as output_file:
        for line in input_file:
            output_file.write(URL + line)

Can you pls update the code for the following. Becouse this code output will go to separate lines.

URL = "www.website.com/users/"
URI_PART = "/set/passwd"

with open('input.txt', 'r') as input_file:
    with open('output.txt', 'w') as output_file:
        for line in input_file:
            new_url = URL + line + URI_PATH
            print(new_url)

Then the output URLs will split to 2 lines. Do you have an idea for this?

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.