-1

Im running python 2.7

import requests

count = 1000
while count <= 10000:
    count += 1
    user = requests.get("https://api.roblox.com/Users/" + str(count)).json()    ['Username']
    print (user)

Thanks!

7
  • fo = open("foo.txt", "wb") fo.write( user) Commented Nov 12, 2016 at 6:48
  • @OmidCompSCI And fo.close() Commented Nov 12, 2016 at 6:50
  • @Rakesh_K Correct. Commented Nov 12, 2016 at 6:51
  • go through this docs.python.org/3.3/tutorial/… Commented Nov 12, 2016 at 6:51
  • 2
    @Rakesh_K> or rather, use with, avoiding exception-related issues. Commented Nov 12, 2016 at 6:54

2 Answers 2

0

Use the open file, in with statement as this:

import requests

count = 1000
with open('output.txt', 'w') as f:
    while count <= 10000:
        count += 1
        user = requests.get("https://api.roblox.com/Users/" + str(count)).json()['Username']
        print (user)
        f.write(user + '\n')
Sign up to request clarification or add additional context in comments.

Comments

0

Use Python's with, to open your output file, this way the file is automatically closed afterwards. Secondly, it makes more sense to use range() to give you all of your numbers, and format can be used to add the number to your URL as follows:

import requests

with open('output.txt', 'w') as f_output:
    for count in range(1, 10000 + 1):
        user = requests.get("https://api.roblox.com/Users/{}".format(count)).json()['Username']
        print(user)
        f_output.write(user + '\n')

Each entry is then written to the file with a newline after each.

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.