首页 > 解决方案 > 搜索每个值时,发送 POST 请求时输出为空列表

问题描述

我曾经在网络中找到form_data 列表,然后发送 POST 请求,但在这种情况下,没有这样的事情,而不是我接受请求有效负载。因此,该方法似乎行不通。

这是该网站的网址: https ://www.e-taxes.gov.az/ebyn/vergiBorcu.html

输入 <input ng-model="" voen="" placeholder="1234567891" type="text" class="form-control ng-valid ng-dirty ng-valid-parse ng-touched ng-empty" id="voen">

这是我尝试过的代码:

import requests
from bs4 import BeautifulSoup
import csv
import json

request_headers = {
    'Accept': 'application/json, text/plain, */*',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'en,en-GB;q=0.9',
    'Connection': 'keep-alive',
    'Content-Length': '50',
    'Content-Type': 'application/json;charset=UTF-8',
    'Cookie': 'ETAXES=etaxes; JSESSIONID=C033E796031CA5C84A3A7B38E24330D4',
    'Host': 'www.e-taxes.gov.az',
    'Origin': 'https://www.e-taxes.gov.az',
    'Referer': 'https://www.e-taxes.gov.az/ebyn/vergiBorcu.html',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/75.0.3770.142 Safari/537.36',

}

taxid = {
    '8400260181',
    '1700393071',
    '1004199751',
    '2002283071',
}

payloads = {"METHOD": "wsEbynGetDebetSum",
            "voen": taxid,
            }

url = 'https://www.e-taxes.gov.az/ebyn/vergiBorcu.html'

for voen in taxid:
    payloads['voen'] = voen
    response = requests.post(url, data=json.dumps(payloads), headers=request_headers)
    s = BeautifulSoup(response.content, 'lxml')
    sContent = s.find('div', class_="col-sm-3").find_all("input", class_="form-control ng-pristine")
    print(voen)
    print(sContent)

with open('taxDebt.csv', 'w', newline='', encoding='utf-8') as myfile:
    writer = csv.writer(myfile, quoting=csv.QUOTE_ALL, delimiter="\n")
    writer.writerow(sContent)

我希望得到类似的输出,Debt: "value"但它返回空列表:

1700393071
[]
1004199751
[]
8400260181
[]
2002283071
[]

标签: pythonbeautifulsouppython-requests

解决方案


我相信您的find语法不正确。您正在寻找一个看起来像的标签,<div class="col-sm-3">为了使用 BeautifulSoup 获得它,试试这个

s.find("div", class_="col-sm-3")

find_all调用也必须修改:

sContent = s.find('div', class_= "col-sm-3").find_all("input", class_= "form-control ng-pristine")

推荐阅读