Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- # Create a session object
- session = requests.Session()
- # Set a legitimate User-Agent
- headers = {
- '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',
- }
- # First request to the site
- url = 'https://example.com/'
- response = session.get(url, headers=headers)
- # If the first request is successful, the session object will store the cookies
- if response.status_code == 200:
- # Now you can use the session object to send further requests with the cookies
- api_url = 'https://example.com/api/your-endpoint'
- headers.update({
- 'Content-Type': 'application/json',
- 'Authorization': 'Bearer your_api_key',
- })
- api_response = session.get(api_url, headers=headers)
- if api_response.status_code == 200:
- data = api_response.json()
- print(data)
- else:
- print(f'Error: {api_response.status_code}')
- else:
- print(f'Error: {response.status_code}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement