ValueError: empty range for randrange() (0, 0, 0)

Question:

I try to export random proxy from a webpage with this script:

def randProxy():
    url = 'https://free-proxy-list.net/anonymous-proxy.html'
    response = requests.get(url)
    parser = fromstring(response.text)
    proxies = []
    for i in parser.xpath('//tbody/tr')[:20]:
        if i.xpath('.//td[7][contains(text(),"yes")]'):
            proxy = ":".join([i.xpath('.//td[1]/text()')[0], i.xpath('.//td[2]/text()')[0]])

        try:
            t = requests.get("https://www.google.com/", proxies={"http": proxy, "https": proxy}, timeout=5)
            if t.status_code == requests.codes.ok:
                proxies.append(proxy)
        except:
            pass
        
        proxy = proxies[random.randint(0, len(proxies)-1)]
        px={"http": proxy, "https": proxy}

randProxy()

but when i try i get this error

Traceback (most recent call last):
  File "C:UsersnatanOneDriveDokumenPrivate-FolderProjectWtDmain.py", line 51, in <module>
    randProxy()
  File "C:UsersnatanOneDriveDokumenPrivate-FolderProjectWtDmain.py", line 33, in randProxy
    proxy = proxies[random.randint(0, len(proxies)-1)]
  File "C:UsersnatanAppDataLocalProgramsPythonPython39librandom.py", line 339, in randint
    return self.randrange(a, b+1)
  File "C:UsersnatanAppDataLocalProgramsPythonPython39librandom.py", line 317, in randrange
    raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width))
ValueError: empty range for randrange() (0, 0, 0)

Are anyone know how to fix this? i only want to get some random proxy

Asked By: Wh4teve3r

||

Answers:

That happens when the second argument is a negative. Look here.

import random
print(random.randint(0,-1))

output

Traceback (most recent call last):
  File "<string>", line 2, in <module>
File "/usr/lib/python3.8/random.py", line 248, in randint
    return self.randrange(a, b+1)
  File "/usr/lib/python3.8/random.py", line 226, in randrange
    raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width))
ValueError: empty range for randrange() (0, 0, 0)

So maybe find a fix for changing the last parameter to something positive.

Answered By: Thavas Antonio

I believe it’s actually because the first number has to be smaller than the second number. I just ran a program and had the same issue with random.randint(-50, -100) then switched the numbers and it worked

Answered By: PythonNick
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.