gpt4 book ai didi

python - 无法在输入框中捕获并输入一些动态生成的数字来填充一些结果

转载 作者:行者123 更新时间:2023-11-28 20:55:27 25 4
gpt4 key购买 nike

您好,我正在尝试从网站https://www.nsekra.com/抓取信息。我们需要从下拉列表中选择Non-Individual,然后输入 PAN 作为 AAAHA0064Q,以及每次访问网站时生成随机数的验证码被访问或打开。之后我们需要按搜索按钮才能获取所需的信息。

import requests
from bs4 import BeautifulSoup

resp = requests.get('https://www.nsekra.com/')
soup = BeautifulSoup(resp.text,'lxml')
dictinfo = {i['name']:i.get('value','') for i in soup.select('input[name]')}

# trying to enter PAN as 'AAAHA0064Q'
dictinfo['txtPan']='AAAHA0064Q'

# trying to get captcha number & passing to textbox
captcha_number = soup.select_one("#lblDynamicCode").text
print('Fetched Catpcha No. -> ',captcha_number);
dictinfo['txtImageBox'] = captcha_number

# passsing pan no. & captcha number to the request method
resp2 = requests.post('https://www.nsekra.com/',data=dictinfo)
soup2 = BeautifulSoup(resp2.text,'lxml')
name = soup2.select_one('#lblKra_name').text
print('KRA Name : '+name)

输出

print('Fetched Catpcha No. -> ',s);

Fetched Catpcha No. -> 757205

print(soup2.prettify());

enter image description here

print('KRA Name : '+name)

KRA Name : &nbsp

预期输出

KRA Name : CVL KRA

正如您所看到的,我能够获取验证码号码,但是当我尝试将其传递到网站时,每次访问网站时它都会重新生成新的号码。所以基本上,上面的代码确实获取了验证码号码,但是在访问网站时,会生成新号码,并且传递的是旧号码或以前的号码,而不是新号码,而不是访问网站时的当前号码。我如何获取并利用动态生成的数字来获取我感兴趣的结果?我喜欢坚持使用 requests 库来完成它。

最佳答案

重要的部分是在 session 中执行请求,而不是将输入中找到的所有参数作为数据发布(只有 btnAllKRA 很重要):

import requests
from bs4 import BeautifulSoup

with requests.Session() as session: # <---- IMPORTANT!

resp = session.get('https://www.nsekra.com/')
soup = BeautifulSoup(resp.text,'lxml')
dictinfo = {i['name']:i.get('value','') for i in soup.select('input[name]')}

# trying to enter PAN as 'AAAHA0064Q'
dictinfo['txtPan']='AAAHA0064Q'

# trying to get captcha number & passing to textbox
captcha_number = soup.select_one("#lblDynamicCode").text
print('Fetched Catpcha No. -> ',captcha_number);
dictinfo['txtImageBox'] = captcha_number

for k in [*dictinfo.keys()]: # <---- IMPORTANT!
if k.lower().startswith('btn') and k.lower() != 'btnallkra':
del dictinfo[k]

# passsing pan no. & captcha number to the request method
resp2 = session.post('https://www.nsekra.com/',data=dictinfo)
soup2 = BeautifulSoup(resp2.text,'lxml')

name = soup2.select_one('#lblKra_name').text
print('KRA Name : '+name)

打印:

Fetched Catpcha No. ->  314885
KRA Name : CVL KRA

关于python - 无法在输入框中捕获并输入一些动态生成的数字来填充一些结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56766434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com