Forums

Is it possible to get page html code?

code:

import requests

url = 'https://example.com'  
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',  # User-Agent dla Chrome
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'
}  # Nagłówki HTTP

try:
    response = requests.get(url, headers=headers)  # Pobieranie strony
    response.raise_for_status()  
    print(response.text)  # Wyświetlenie kodu HTML
except requests.exceptions.RequestException as e:
    print("Error", e)

On my pc this code working but on pythonanywhere i have errors

If you check the response code you'll probably see it is a 403 - https://help.pythonanywhere.com/pages/403ForbiddenError/