You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
r.text# You can parse this using html parser, we have html/source code right nowprint(r.status_code) # gives the status codeprint(r.ok) # returns True if below 400print(r.headers) # Gives the header
Downloading image / Getting data in bytes
r.content# Gives data in bytes# Writing the byte data to get an image, just like an image downloadimportrequestsr=requests.get('https://imgs.xkcd.com/comics/python.png')
withopen('comic.png', 'wb') asf:
f.write(r.content)
get request with params
importrequestspayload= {'page':2, 'count':25}
r=requests.get('https://httpbin.org/get', params=payload)
print(r.text)
print(r.url) # To get te url inform of error prone format
post request with params
importrequestsimportjsonpayload= {'username':'kush', 'password':'testing'}
r=requests.post('https://httpbin.org/post', data=json.dumps(payload))
print(r.json()) # It returns the dict format# Example 2importrequestsimportjsonpayload= {'username':'kush', 'email':'mail@mail83', 'password':'123456'}
headers= {'content-type': 'application/json'}
url='https://painhost99.herokuapp.com/user/signup'r=requests.post(url, data=json.dumps(payload), headers=headers)
Authentication code
importrequestsr=requests.get('https://httpbin.org/basic-auth/kush/123456', auth=('kush', '123456'), timeout=3)
print(r.text) # returns a response