首页 > 解决方案 > 尝试使用 Selenium 时不允许使用方法 (POST)

问题描述

我试图让这个 django 应用程序与 selenium 一起工作,但它根本不起作用。代码现在有点乱,因为我试图弄清楚如何做我需要做的事情(我必须用 Selenium 编写一个网络抓取脚本,从政府那里获取此站点中的人的商业 ID 号,一次他们输入它,在下面应该出现与政府 api 中可用的 id 对应的人的姓名和姓氏。老实说,我不知道如何做到这一点,所以我正在尝试)。现在的问题是,每次我向表单提交随机数时,我都会不断收到 Method Not Allowed (POST): /constancia-inscripcion 和 Method Not Allowed: /constancia-inscripcion (405)。老实说,我认为错误可能在任何地方,所以我需要一些帮助才能找到它。

这是我的看法:

from django.shortcuts import render, HttpResponse
import requests
from django.views import View

from selenium import webdriver

class ConstanciaInscripcion(View):
    
    DRIVER_PATH = 'C:/Users/micae/Documents/python/chromedriver.exe'
    driver = webdriver.Chrome(executable_path='C:/Users/micae/Documents/python/chromedriver.exe')
    driver.get('https://seti.afip.gob.ar/padron-puc-constancia-internet/ConsultaConstanciaAction.do')
    
    def get(self, request):
       return render(request, 'app/constancia-inscripcion.html')
       
    def cuit(request):
        
        information = []

        if request.method == 'POST':
            
            driver.find_element_by_xpath("//label[@class='cuit']/input[@class='word' and @type='text' and @value='']").click()
            driver.find_element_by_xpath("//label[@class='cuit']/input[@class='word' and @type='text' and @value='']").clear()
            driver.find_element_by_xpath("//label[@class='cuit']/input[@class='word' and @type='text' and @value='']").send_keys("")

            a5_url = 'https://aws.afip.gov.ar/srpadron/webservices/personaServiceA5'

            search_params: {
                'persona' : 'a5:getPersona',
                'token' : 'token',
                'sign'  : 'sign',
                'cuit' : 'cuitRepresentada',
                'q' : request.POST['search']
            }

            r = requests.get(a5_url, params=search_params)
            results = r.json()

            if len(results):
                for result in results:
                    person_data = {
                    'nombre' : result['nombre'],
                    'apellido': result['apellido'],
            }
                information.append(person_data)  
        
            else:
                message = print('El CUIT ingrasado es incorrecto')

            print(information)
     

        context = {
        'information' : information
        }

        return render(request,'app/constanciainscripcion.html', context)
          
        
    def email(request):

        if request.method == 'POST':
            form = ClientForm(request.POST)
            if form.is_valid():
                # process form data
                obj = Listing() #gets new object
            
                obj.email = form.cleaned_data['email']
                #finally save the object in db
                obj.save()

        return render(request, 'app/constancia-inscripcion.html')

    def captcha(request):
        
        if request.method == 'POST':
            captcha_code = driver.find_element_by_xpath("//*[@id='CaptchaCode']").send_keys(CAPTCHA)
            captcha_field = driver.find_element_by_xpath("//*[@id='captchaField']").send_keys(CAPTCHA-FIELD)
            submit = driver.find_element_by_xpath("//*[@id='btnConsultar']").click()
            
            RECAPTCHA_SITE_KEY="6LdEnNsZAAAAAKotfjwOJsR4r8nvq5ADWhMZEqJ9"
            RECAPTCHA_SECRET_KEY="6LdEnNsZAAAAANeDqX4oOOaPnidRtoG9yMApCl_t"
    
        c_data = {
            'response': request.POST.get('g-recaptcha-response'),
            'secret': RECAPTCHA_SECRET_KEY
            }
    
        resp = requests.post('https://www.google.com/recaptcha/api/siteverify', data=c_data)
        captcha_response = resp.json()
 
        if not captcha_response.get('success') or captcha_response.get('score') < 0.6:
            print('Captcha incorrecto')
        else:
            print('Captcha correcto')

    
    def send(request):

        if request.method == 'POST':
            print('Loading')

            if not EnvironmentError:
                print( 'Sus datos fueron enviados correctamente, en un máximo de 2 hs. estarás recibiendo tu constancia por email.')
            else:
                print('No hemos podido procesar su solicitud, por favor reintente nuevamente más tarde.')

        
        return render(request, 'app/constancia-inscripcion.html')

这是我的网址:

from django.urls import path
from . import views

app_name = 'app'

urlpatterns = [
    path('constancia-inscripcion', views.ConstanciaInscripcion.as_view(), name='constancia-inscripcion')

]

标签: pythondjangoselenium

解决方案


推荐阅读