Advertisement
Guest User

kal

a guest
Mar 28th, 2023
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. import requests
  2.  
  3. # Create a session object
  4. session = requests.Session()
  5.  
  6. # Set a legitimate User-Agent
  7. headers = {
  8.     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
  9. }
  10.  
  11. # First request to the site
  12. url = 'https://example.com/'
  13. response = session.get(url, headers=headers)
  14.  
  15. # If the first request is successful, the session object will store the cookies
  16. if response.status_code == 200:
  17.     # Now you can use the session object to send further requests with the cookies
  18.     api_url = 'https://example.com/api/your-endpoint'
  19.     headers.update({
  20.         'Content-Type': 'application/json',
  21.         'Authorization': 'Bearer your_api_key',
  22.     })
  23.    
  24.     api_response = session.get(api_url, headers=headers)
  25.    
  26.     if api_response.status_code == 200:
  27.         data = api_response.json()
  28.         print(data)
  29.     else:
  30.         print(f'Error: {api_response.status_code}')
  31. else:
  32.     print(f'Error: {response.status_code}')
  33.  
Tags: govno
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement