在发送POST请求时,如何给出搜索数据在循环中返回错误消息的异常?

2024-09-30 01:28:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有ID,他们每个人都在网站表单中发送HTTP POST请求,但其中一些返回错误,整个语法停止返回剩余ID的输出。该网站必须广播酒吧,其中一个是公司(L)和个人(P)在名单上命名为“小费”。 代码应该检查ID('voen')是否与“tip”L(对于个人)相关,然后在网站中搜索ID时,L的形式(在表单数据列表中,您可以看到名为“tip”的键被引用到tip list

import requests
from bs4 import BeautifulSoup

request_headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'en-US,en;q=0.9',
    'Cache-Control': 'max-age=0',
    'Connection': 'keep-alive',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Host': 'www.e-taxes.gov.az',
    'Origin': 'https://www.e-taxes.gov.az',
    'Referer': 'https://www.e-taxes.gov.az/ebyn/payerOrVoenChecker.jsp',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-Site': 'same-origin',
    'Sec-Fetch-User': '?1',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'YOUR USER AGENT',
}

voens = [1700401281,
         4501313952,
         ]

tip = ['L',
       'P',
       ]

form_data = {
    'tip': tip,
    'voenOrName': 'V',
    'voen': voens,
    'name': '',
    'submit': '  Yoxla   ',
}


url = 'https://www.e-taxes.gov.az/ebyn/payerOrVoenChecker.jsp'

for voen in voens:
    form_data['voen'] = voen
    response = requests.post(url, data=form_data, headers=request_headers)
    s = BeautifulSoup(response.content, 'lxml')
    sContent = s.findAll('table', {'class': 'com'})[0].findAll('tr', recursive=False)[1]
    print(type(sContent))
    **if sContent:**
        outcome = sContent.get_text().strip()
        print(outcome)
    else:
        form_data['tip'] = tip[1]
        response = requests.post(url, data=form_data, headers=request_headers)
        sContent = s.findAll('table', {'class': 'com'})[0].findAll('tr', recursive=False)[1]
        print(sContent)

在“voens”字典上面的代码中,有一个公司id和一个个人id,在代码中,我编写它就像如果id不是公司的,那么检查另一个“提示”(p) 我把问题写在if sContent:行中 错误消息:sContent = s.findAll('table', {'class': 'com'})[0].findAll('tr', recursive=False)[1] IndexError: list index out of rang


Tags: formiddataapplication网站wwwazheaders

热门问题