HTTP Error 403 Forbidden
http error 403: forbidden occurs when you try to scrap a webpage using urllib.request module and the mod_security blocks the request.
The 403 response belongs to the 4xx range of HTTP responses: Client errors.
The easy way to resolve the error is by passing a valid user-agent as a header parameter
## 수정 전
from urllib.request import urlopen, Request
url = "https://suwoni-codelab.com/assets/story.txt"
with urlopen(url) as story:
for line in story:
print(line)
HTTPError: HTTP Error 403: Forbidden
## 수정 후
from urllib.request import urlopen, Request
url = "https://suwoni-codelab.com/assets/story.txt"
header ={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"}
response = Request(url, headers=header)
with urlopen(response) as story:
for line in story:
print(line)
출처: https://itsmycode.com/python-urllib-error-httperror-http-error-403-forbidden/