Почему появляется ошибка List index out of range?

proxylist = []
proxytype = "http"
url = 'https://icanhazip.com/'




with open('proxy.txt', encoding="UTF-8") as proxies:
    for proxy in proxies.readlines():
        proxylist.append(proxy)


while True:

    proxyaddres = random.choice(proxylist)

    proxies = {
        'http': f'{proxytype}://{proxyaddres}',
        'https': f'{proxytype}://{proxyaddres}'
    }
    try:
        print(proxies)
        response = requests.get(url, proxies=proxies, timeout=5)
        print(response.text)
        break

    except Exception as e:
        print(e)
        continue


def parsing():
    print("Начинаю парсинг...")
    pars_req = requests.get(f'https://scrap.tf/raffles', cookies=cj, headers=headers)
    print(pars_req.status_code)
    html = pars_req.text
    htmlparser = etree.HTMLParser()
    tree = etree.XML(html, htmlparser)
    my_list = []
    m = 1

    while True:
        try:
            pars = tree.xpath(f'//html/body/div[4]/div[2]/div[3]/div[1]/div[{int(m)}]/div[1]/div[1]/a/@href')[0]
            my_list.append(pars)
            m = m + 1
            continue
        except:

            break

    z = 0
    while True:
        result = enter_raffel(my_list[z])
        print(f"Результат: {result}")
        z = z + 1
        time.sleep(3)

        if z == len(my_list):
            print("Я закончил. Ухожу спать на 15 минут")

            time.sleep(900)
            break
    parsing()


def enter_raffel(code):
    try:
        # print(my_list)
        req = requests.get(f'https://scrap.tf{code}', cookies=cj, headers=headers, proxies=proxies, timeout=20)
        print(req.status_code)
        html = req.text
        htmlparser = etree.HTMLParser()
        tree = etree.XML(html, htmlparser)
        token = tree.xpath('//html/body/script[16]/text()')[0]
        token_two = tree.xpath('//*[@id="main-container"]/div/div[2]/div[5]/div[2]/button[2]/@onclick')[0]
        token_two_revers = token_two.split(", ")[1]
        token_two_revers_two = token_two_revers.replace("'", "")  # csrf
        token_revers = token.split('ScrapTF.User.Hash = "')[1]
        token_revers_two = token_revers.split('";')[0]  # hash
        data = {
            'raffle': f'{code.split("/")[2]}',
            'captcha': '',
            'hash': f'{token_two_revers_two}',
            'flag': 'false',
            'csrf': f'{token_revers_two}',
        }
        if req.status_code == 403:
            with open("failed.html", "w", encoding="utf-8") as f:
                f.write(req.text)
        elif req.status_code == 200:
            with open("success.html", "w", encoding="utf-8") as f:
                f.write(req.text)
        response = requests.post('https://scrap.tf/ajax/viewraffle/EnterRaffle', cookies=cj, headers=headers,
                                 data=data, proxies=proxies)
        result = json.loads(response.text)

        return f"https://scrap.tf{code} - {result['message']}"

    except Exception as er:

        return f"Ошибка обработки - {er}, {code}"


parsing()

Почему возникает данная ошибка и как её исправить?


Ответы (0 шт):