python 3.x - Requests.history Not printing python3 -
i'm noob python programming. have simple problem. im using python3, in pycharm 2017.2.1, want print out request redirects. have googled seem doing right. no errors , response 200. missing?
import requests url='http://httpbin.org/html' payload= {'url':'http://bing.com'} req = requests.get(url, params=payload) #print(req.text) print("response code: " + str(req.status_code)) if req.history: print ("request redirected") resp in req.history: print (resp.status_code, resp.url) print ( "final destination:") print(resp.status_code, resp.url) else: print("request not redirected") x in req.history: print(str(x.status_code) + ' : ' + x.url)
output:
response code: 200 process finished exit code 0
i think should use url: http://httpbin.org/redirect-to
, url you're using not redirect request. else block not executed because applies loop.
import requests url='http://httpbin.org/redirect-to' payload= {'url':'http://bing.com'} req = requests.get(url, params=payload) print("response code: " + str(req.status_code)) if req.history: print ("request redirected") resp in req.history: print("final destination:") print(resp.status_code, resp.url) else: print("request not redirected") x in req.history: print(str(x.status_code) + ' : ' + x.url)
note can disable redirects if set allow_redirects
parameter false
Comments
Post a Comment