urllib.error.HTTPError: HTTP Error 403: Forbidden in my web scrapping

I try to do a webscrapping script who gives me if a website is a wordpress or no, but i get this error:

urllib.error.HTTPError: HTTP Error 403: Forbidden

and i don’t understand, i use this headers who is supposed to pass it (in other stacks overflow):

   headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:66.0) Gecko/20100101 Firefox/66.0", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language": "fr-fr,en;q=0.5", "Accept-Encoding": "gzip, deflate", "DNT": "1", "Connection": "close", "Upgrade-Insecure-Requests": "1"}

there is my function;

def check_web_wp(url):
    is_wordpress = False
    print(repr(url))
    headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:66.0) Gecko/20100101 Firefox/66.0", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language": "fr-fr,en;q=0.5", "Accept-Encoding": "gzip, deflate", "DNT": "1", "Connection": "close", "Upgrade-Insecure-Requests": "1"}

    response = requests.get(url, headers=headers)

    with urllib.request.urlopen(url) as response:
        texte = response.read()
        poste_string = str(texte)
        splitted = poste_string.split()
    
        for word in splitted:
            if ("wordpress" in word):
                is_wordpress = True
                break
            
    return is_wordpress


def main():
    url = "https://icalendrier.fr/"
    is_wp = check_web_wp(url)

did i miss something? Is it the website who is too much “securised”?

Thanks for yours answers

Answer

(As requested, my comment as answer)

Your line with urllib.request.urlopen(url) as response: (without the headers) is overwriting your previous response object from response = requests.get(url, headers=headers) (with headers).

Use requests only instead of urllib, like so:

def check_web_wp_fixed(url):
    is_wordpress = False
    headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:66.0) Gecko/20100101 Firefox/66.0", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language": "fr-fr,en;q=0.5", "Accept-Encoding": "gzip, deflate", "DNT": "1", "Connection": "close", "Upgrade-Insecure-Requests": "1"}

    response = requests.get(url, headers=headers)
    splitted = response.text.split()
    
    for word in splitted:
        if ("wordpress" in word):
            is_wordpress = True
            break
            
    return is_wordpress

(Only made it working, didn’t check to see if the code could be optimized in any way)