如何使用Requests POST方法从网站获取搜索结果?

2024-09-30 01:26:35 发布

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

我试图得到的网站搜索结果的输出,我用请求后的方法来做它。下面您可以看到表单和从url输入的htmls。我需要在表单的输入中得到每个搜索结果。你知道吗

我尝试了下面的代码,但没有返回任何结果

import requests
from bs4 import BeautifulSoup

# FORM from website
# <form name="form1" method="post" action="payerOrVoenChecker.jsp">

# INPUT from the website
# <input type="text" name="voen" size="38" style="BACKGROUND-COLOR: #ffffff; BORDER-BOTTOM-STYLE: groove;
# BORDER-LEFT-STYLE: groove; BORDER-RIGHT-STYLE: groove; BORDER-TOP-STYLE: groove; COLOR: #000000; FONT-FAMILY:
# Tahoma, Arial; FONT-SIZE: 12px" value="">

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 = {2000460031,
         1000877741,
         1000877741,
         1500403661,
         1000877741,
         3000489411,
         1000877741,
         1802142932,
         }

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
    form_data['tip'] = tip
    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]
    outcome = sContent.get_text().strip()
    # .find("tr", recursive=False)
    print(outcome)

预期结果将以表格形式出现,我在网站中添加搜索前后的截图,突出显示的是我需要获得的表格

enter image description hereenter image description here


Tags: namefromformurldataapplicationstylewww
1条回答
网友
1楼 · 发布于 2024-09-30 01:26:35

您缺少了POST请求主体中要发送的其他项。你知道吗

试试这个:

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',
}

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

response = requests.post(url, data=form_data, headers=request_headers)

解决这些问题的最佳方法是切换到浏览器中开发人员工具中的“网络”选项卡。Chrome和Firefox的快捷方式都是F12。你知道吗

  • 打开开发人员工具后,转到“网络”选项卡。你知道吗
  • 现在,提交所有您想要自动化的请求(例如,在本例中,在填写ID之后单击submit)。你知道吗

它将显示浏览器在幕后发送的所有请求的列表。单击与您的URL匹配的一个。你知道吗

右边将打开一个窗格,显示使用了什么方法(GET/POST)、请求中传递了什么头、发送了什么数据(如果是POST)等等

我所做的只是从该选项卡粘贴请求头和表单数据。你知道吗

inspecting network tab

相关问题 更多 >

    热门问题