0

J'ai essayé de comprendre comment je n'utilise pas ce code correctement. J'ai fait cela de deux manières différentes, y compris les déclarations WITH et en utilisant directement urllib3. Il semble que j'essaie d'afficher les données et qu'il se connecte à une redirection ou qu'il ne soit pas publié du tout, puis il échoue toujours. J'ai énuméré le code ci-dessous plus de multiples façons que je vérifie ce que je reçois de chrome et de la CLI. Il semble y avoir un cookie de réponse que je ne manipule pas aussi loin que je peux dire. Pouvez-vous s'il vous plaît m'aider à voir ce que je suis en train de me tromper? Divulgation: Certaines informations ont été modifiées, mais devraient suffire ici pour voir ce qui se passe.Comprendre les redirections de réponse à après le post

import requests 
import json 


base = "http://www.XXXXXX.com/" 
url = "http://www.XXXXXX.com/login.php" 
scraped_url = "http://www.XXXXXX.com/cart.php?mode=wishlist" 
headers = {'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36', 
      "Connection":"keep-alive", 
      "mode":"login", 
      "Accept-Language":"en-US, en;q=0.8", 
      "Accept-Encoding":"gzip, deflate, sdch", 
      "store_language":"en", 
      "RefererCookie":"deleted",#http%3A%2F%2Fwww.XXXXXX.com%2Fhome.php 
      "Origin":"http://www.XXXXXX.com", 
      "Upgrade-Insecure-Requests": "1", 
      "P3P": "CP=NON CURa ADMa DEVa TAIa CONi OUR DELa BUS IND PHY ONL UNI PUR COM NAV DEM STA", 
      "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8." 
      } 


data = {"password":"Reallybadpassword", 
      "username":"[email protected]"} 


jar = requests.cookies.RequestsCookieJar() 

req = requests.Request('GET', 'http://www.XXXXXX.com/login.php') 
r = req.prepare() 
s = requests.Session() 
s.send(r) 
s.headers.update 
s.jar.update 

r = s.get(base, cookies=jar) 
s.headers.update 
jar.update 
print(r.history) 
print(r.status_code) 
print(r.url) 
print(r.json) 
print("################################################################################") 
print(r.headers) 
print("********************************************************************************") 
print(r.cookies) 
print("////////////////////////////////////////////////////////////////////////////////") 


r = s.post(url, params=data, cookies=jar, allow_redirects=True, headers=headers) #params=values, 
s.headers.update 
jar.update 

print(r.history) 
print(r.status_code) 
print(r.url) 
print(r.json) 
print("################################################################################") 
print(r.headers) 
print("********************************************************************************") 
print(r.cookies) 
print("////////////////////////////////////////////////////////////////////////////////") 


r = s.get(scraped_url, params=data, cookies=jar) 
s.headers.update 
jar.update 

print(r.history) 
print(r.status_code) 
print(r.url) 
print(r.json) 
print("################################################################################") 
print(r.headers) 
print("********************************************************************************") 
print(r.cookies) 
print("////////////////////////////////////////////////////////////////////////////////") 


s.close() 

    #####-RESPONSE FROM SITE TO CLI###### 
    [email protected]:/media/Iam/Drive/Backup/Documents/TestWebpages$ python3 login_scraper.py 
    [] 
    200 
    http://www.XXXXXX.com/ 
    <bound method Response.json of <Response [200]>> 
    ################################################################################ 
    CaseInsensitiveDict({'pragma': 'no-cache', 'server': 'nginx', 'last-modified': ' 
    Sun, 23 Apr 2017 20:33:45 GMT', 'content-type': 'text/html; charset=iso-8859-1', 
    'vary': 'Accept-Encoding', 'p3p': 'CP="NON CURa ADMa DEVa TAIa CONi OUR DELa BU 
    S IND PHY ONL UNI PUR COM NAV DEM STA"', 'set-cookie': 'xid_eb442=e8278c9dc379c 
    6dfae862b7bf2721138; path=/; domain=www.XXXXXX.com; httponly, RefererCookie=del 
    eted; expires=Sat, 23-Apr-2016 20:33:44 GMT; path=/; domain=www.XXXXXX.com; htt 
    ponly', 'date': 'Sun, 23 Apr 2017 20:33:45 GMT', 'transfer-encoding': 'chunked' 
    , 'expires': 'Mon, 26 Jul 1997 05:00:00 GMT', 'connection': 'keep-alive', 'x-po 
    wered-by': 'PleskLin', 'cache-control': 'no-store, no-cache, must-revalidate, p 
    ost-check=0, pre-check=0', 'content-encoding': 'gzip'}) 
    ******************************************************************************** 
    <RequestsCookieJar[<Cookie xid_eb442=e8278c9dc379c6dfae862b7bf2721138 for .www.XXXXXX.com/>]> 
    //////////////////////////////////////////////////////////////////////////////// 
    [] 
    200 
    http://www.XXXXXX.com/login.php 
    <bound method Response.json of <Response [200]>> 
    ################################################################################ 
    CaseInsensitiveDict({'pragma': 'no-cache', 'server': 'nginx', 'last-modified': ' 
    Sun, 23 Apr 2017 20:33:45 GMT', 'content-type': 'text/html; charset=iso-8859-1', 
    'vary': 'Accept-Encoding', 'p3p': 'CP="NON CURa ADMa DEVa TAIa CONi OUR DELa BU 
    S IND PHY ONL UNI PUR COM NAV DEM STA"', 'set-cookie': 'xid_eb442=649b55131f4f7 
    e9d6958560d3ed406c0; path=/; domain=www.XXXXXX.com; httponly, RefererCookie=del 
    eted; expires=Sat, 23-Apr-2016 20:33:44 GMT; path=/; domain=www.XXXXXX.com; htt 
    ponly', 'date': 'Sun, 23 Apr 2017 20:33:45 GMT', 'transfer-encoding': 'chunked' 
    , 'expires': 'Mon, 26 Jul 1997 05:00:00 GMT', 'connection': 'keep-alive', 'x-po 
    wered-by': 'PleskLin', 'cache-control': 'no-store, no-cache, must-revalidate, p 
    ost-check=0, pre-check=0', 'content-encoding': 'gzip'}) 
    ******************************************************************************** 
    <RequestsCookieJar[<Cookie xid_eb442=649b55131695e9ff48560d3ed406c0 for .www.XXXXXX.com/>]> 
    //////////////////////////////////////////////////////////////////////////////// 
    (<Response [302]>,) 
    200 
    http://www.XXXXXX.com/login.php 
    <bound method Response.json of <Response [200]>> 
    ################################################################################ 
    CaseInsensitiveDict({'pragma': 'no-cache', 'server': 'nginx', 'last-modified': ' 
    Sun, 23 Apr 2017 20:33:45 GMT', 'content-type': 'text/html; charset=iso-8859-1', 
    'vary': 'Accept-Encoding', 'p3p': 'CP="NON CURa ADMa DEVa TAIa CONi OUR DELa BU 
    S IND PHY ONL UNI PUR COM NAV DEM STA"', 'set-cookie': 'xid_eb442=649b55131f4f7 
    e9d6958560d3ed406c0; path=/; domain=www.XXXXXX.com; httponly, RefererCookie=del 
    eted; expires=Sat, 23-Apr-2016 20:33:45 GMT; path=/; domain=www.XXXXXX.com; htt 
    ponly', 'date': 'Sun, 23 Apr 2017 20:33:46 GMT', 'transfer-encoding': 'chunked' 
    , 'expires': 'Mon, 26 Jul 1997 05:00:00 GMT', 'connection': 'keep-alive', 'x-po 
    wered-by': 'PleskLin', 'cache-control': 'no-store, no-cache, must-revalidate, p 
    ost-check=0, pre-check=0', 'content-encoding': 'gzip'}) 
    ******************************************************************************** 
    <RequestsCookieJar[<Cookie xid_eb442=649b55131695e9ff48560d3ed406c0 for .www.XXXXXX.com/>]> 
    //////////////////////////////////////////////////////////////////////////////// 
    [email protected]:/media/Iam/Drive/Backup/Documents/TestWebpages$ 











    #####-FROM CHROME LOGIN-###### 

    [email protected]:~$ sudo ngrep -W byline -d any -q "XXXXXX" 
    interface: any 
    match: XXXXXX 

    T 192.168.1.5:56250 -> 22.156.106.185:80 [AP] 
    POST /login.php HTTP/1.1. 
    Host: www.XXXXXX.com. 
    Connection: keep-alive. 
    Content-Length: 133. 
    Cache-Control: max-age=0. 
    Origin: http://www.XXXXXX.com. 
    Upgrade-Insecure-Requests: 1. 
    User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36. 
    Content-Type: application/x-www-form-urlencoded. 
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8. 
    Referer: http://www.XXXXXX.com/home.php. 
    Accept-Encoding: gzip, deflate. 
    Accept-Language: en-US,en;q=0.8. 
    Cookie: store_language=en; xid_eb442C_remember=TheEmail%40Gmail.com; RefererCookie=http%3A%2F%2Fwww.XXXXXX.com%2Fhome.php; GreetingCookie=Mr.+John+Doe; xid_eb442=649b55131695e9ff48560d3ed406c0. 
    . 
    xid_eb442=649b55131695e9ff48560d3ed406c0&is_remember=&mode=login&username=TheEmail%40Gmail.com&password=Reallybadpassword 

    T 22.156.106.185:80 -> 192.168.1.5:56250 [AP] 
    HTTP/1.1 302 Found. 
    Server: nginx. 
    Date: Sun, 23 Apr 2017 20:03:04 GMT. 
    Content-Type: text/html; charset=iso-8859-1. 
    Transfer-Encoding: chunked. 
    Connection: keep-alive. 
    Expires: Mon, 26 Jul 1997 05:00:00 GMT. 
    Last-Modified: Sun, 23 Apr 2017 20:03:03 GMT. 
    Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0. 
    Pragma: no-cache. 
    P3P: CP="NON CURa ADMa DEVa TAIa CONi OUR DELa BUS IND PHY ONL UNI PUR COM NAV DEM STA". 
    Set-Cookie: xid_eb442=7e00038d2gd56gtyh73cb97b1225afe; path=/; domain=www.XXXXXX.com; httponly. 
    Set-Cookie: GreetingCookie=Mr.+John+Doe; expires=Fri, 20-Oct-2017 20:03:03 GMT; path=/; domain=www.XXXXXX.com; httponly. 
    Location: http://www.XXXXXX.com/home.php. 
    X-Powered-By: PleskLin. 
    . 
    d4. 
    <br /><br />If the page is not updated in 2 seconds, please follow this link: <a href="http://www.XXXXXX.com/home.php">continue &gt;&gt;</a><meta http-equiv="Refresh" content="0;URL=http://www.XXXXXX.com/home.php" />. 


    T 192.168.1.5:56250 -> 22.156.106.185:80 [AP] 
    GET /home.php HTTP/1.1. 
    Host: www.XXXXXX.com. 
    Connection: keep-alive. 
    Cache-Control: max-age=0. 
    Upgrade-Insecure-Requests: 1. 
    User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36. 
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8. 
    Referer: http://www.XXXXXX.com/home.php. 
    Accept-Encoding: gzip, deflate, sdch. 
    Accept-Language: en-US,en;q=0.8. 
    Cookie: store_language=en; xid_eb442C_remember=TheEmail%40Gmail.com; RefererCookie=http%3A%2F%2Fwww.XXXXXX.com%2Fhome.php; xid_eb442=7e00038d2gd56gtyh73cb97b1225afe; GreetingCookie=Mr.+John+Doe. 
    . 


    T 22.156.106.185:80 -> 192.168.1.5:56250 [A] 
    HTTP/1.1 200 OK. 
    Server: nginx. 
    Date: Sun, 23 Apr 2017 20:03:04 GMT. 
    Content-Type: text/html; charset=iso-8859-1. 
    Transfer-Encoding: chunked. 
    Connection: keep-alive. 
    Vary: Accept-Encoding. 
    Expires: Mon, 26 Jul 1997 05:00:00 GMT. 
    Last-Modified: Sun, 23 Apr 2017 20:03:04 GMT. 
    Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0. 
    Pragma: no-cache. 
    P3P: CP="NON CURa ADMa DEVa TAIa CONi OUR DELa BUS IND PHY ONL UNI PUR COM NAV DEM STA". 
    Set-Cookie: xid_eb442=7e00038d2gd56gtyh73cb97b1225afe; path=/; domain=www.XXXXXX.com; httponly. 
    X-Powered-By: PleskLin. 
    Content-Encoding: gzip. 


    T 192.168.1.5:56250 -> 22.156.106.185:80 [AP] 
    GET /home.php HTTP/1.1. 
    Host: www.XXXXXX.com. 
    Connection: keep-alive. 
    Cache-Control: max-age=0. 
    Upgrade-Insecure-Requests: 1. 
    User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36. 
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8. 
    Referer: http://www.XXXXXX.com/home.php. 
    Accept-Encoding: gzip, deflate, sdch. 
    Accept-Language: en-US,en;q=0.8. 
    Cookie: store_language=en; xid_eb442C_remember=TheEmail%40Gmail.com; RefererCookie=http%3A%2F%2Fwww.XXXXXX.com%2Fhome.php; xid_eb442=7e00038d2gd56gtyh73cb97b1225afe; GreetingCookie=Mr.+John+Doe. 
    . 


    T 22.156.106.185:80 -> 192.168.1.5:56250 [A] 
    HTTP/1.1 200 OK. 
    Server: nginx. 
    Date: Sun, 23 Apr 2017 20:03:04 GMT. 
    Content-Type: text/html; charset=iso-8859-1. 
    Transfer-Encoding: chunked. 
    Connection: keep-alive. 
    Vary: Accept-Encoding. 
    Expires: Mon, 26 Jul 1997 05:00:00 GMT. 
    Last-Modified: Sun, 23 Apr 2017 20:03:04 GMT. 
    Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0. 
    Pragma: no-cache. 
    P3P: CP="NON CURa ADMa DEVa TAIa CONi OUR DELa BUS IND PHY ONL UNI PUR COM NAV DEM STA". 
    Set-Cookie: xid_eb442=7e00038d2gd56gtyh73cb97b1225afe; path=/; domain=www.XXXXXX.com; httponly. 
    X-Powered-By: PleskLin. 
    Content-Encoding: gzip. 

Répondre

0

Compris la pièce manquante. J'espère que cela aidera quelqu'un dans le futur. Le formulaire de données passe le mode: login pas l'en-tête ce changement unique fait tout fonctionner.

têtes = { 'User-Agent': Mozilla/5.0 (X11, Linux x86_64) AppleWebKit/537,36 (KHTML, comme Gecko) Chrome/Safari 57.0.2987.110/537,36 ', "Connexion": "keep- vivant ", " Accept-Language ":" fr-FR, en; q = 0.8 ", " Accepter-Codage ":" gzip, deflate, sdch ", " store_language ":" en ", " RefererCookie " : "supprimé", # http% 3A% 2F% 2Fwww.XXXXXX.com% 2Fhome.php "Origine": "http://www.XXXXXX.com", "Upgrade-Insecure-Requests": "1", "P3P": "CP = NON CURa ADMa DEVa TAIa CONI NOTRE DELA BUS IND PHY ONL UNI PUR COM DEM DEM STA ", " Accepter ":" texte/html, application/xhtml + xml, application/xml; q = 0.9, image/webp, /; q = 0,8. " }

data = { "mot de passe": "Reallybadpassword", "nom d'utilisateur": "[email protected]", "mode": "login", }