-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!

Misha Eide
  • 47
  • 1
  • 5

2 Answers2

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')
sal
  • 3,515
  • 1
  • 10
  • 21
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.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97