BASHA TECH

HTTPError: HTTP Error 403: Forbidden 해결!!!! 본문

Error

HTTPError: HTTP Error 403: Forbidden 해결!!!!

Basha 2022. 9. 30. 14:32
728x90

지난번과 달리 이번엔 에러를 해결했다. 해당 링크를 브라우저에서 들어갔을 때는 정상 접속이 되어 url이 변경되지 않았다는 사실 + python으로 들어갔을 때만 403 에러가 난다는 것을 알았다.

 

# 메인 페이지 정보
url = url_base + url_sub
html = urlopen(url)
# 파싱
soup = BeautifulSoup(html, 'html.parser')

Output exceeds the size limit. Open the full output data in a text editor

--------------------------------------------------------------------------- HTTPError Traceback (most recent call last) d:\big15\pandas-dev\workspace\03 시카고 샌드위치 맛집 분석.ipynb 셀 25 in <cell line: 3>() 1 # 메인 페이지 정보 2 url = url_base + url_sub ----> 3 html = urlopen(url) 4 # 파싱 5 soup = BeautifulSoup(html, 'html.parser') File d:\Anaconda3\envs\pandas-dev\lib\urllib\request.py:222, in urlopen(url, data, timeout, cafile, capath, cadefault, context) 220 else: 221 opener = _opener --> 222 return opener.open(url, data, timeout) File d:\Anaconda3\envs\pandas-dev\lib\urllib\request.py:531, in OpenerDirector.open(self, fullurl, data, timeout) 529 for processor in self.process_response.get(protocol, []): 530 meth = getattr(processor, meth_name) --> 531 response = meth(req, response) 533 return response File d:\Anaconda3\envs\pandas-dev\lib\urllib\request.py:640, in HTTPErrorProcessor.http_response(self, request, response) 637 # According to RFC 2616, "2xx" code indicates that the client's 638 # request was successfully received, understood, and accepted. 639 if not (200 <= code < 300): --> 640 response = self.parent.error(
...
File d:\Anaconda3\envs\pandas-dev\lib\urllib\request.py:649, in HTTPDefaultErrorHandler.http_error_default(self, req, fp, code, msg, hdrs) 648 def http_error_default(self, req, fp, code, msg, hdrs): --> 649 raise HTTPError(req.full_url, code, msg, hdrs, fp) HTTPError: HTTP Error 403: Forbidden

 

해결법

# 메인 페이지 정보
url = url_base + url_sub

# 403 해결
from urllib.error import URLError, HTTPError
import urllib.request

try:
    headers = {'User-Agent' : 'Chrome/105.0.5195.128'} #버전=> chrome://settings/help
    # 이걸 설정 안하면 headers = {'User-Agent' : 'python'}으로 들어감.
    req = urllib.request.Request(url, headers=headers) # headers를 받아서 request해라
    # html = urlopen(url)
    html = urlopen(req)
except HTTPError as e:
    err =  e.read()
    code = e.getcode()
    print(err, code)

웹에서 들어가는 것으로 설정을 해주는 방법으로 우회해서 접근 함.

# 파싱
soup = BeautifulSoup(html, 'html.parser')
soup

정상적으로 결과창이 뜨는 것을 볼 수 있다.

 => 이것이 결과창!

Output exceeds the size limit. Open the full output data in a text editor
<!DOCTYPE html>

<html lang="en-US">
<head>
<meta charset="utf-8"/>
<meta content="IE=edge" http-equiv="X-UA-Compatible">
<link href="https://gmpg.org/xfn/11" rel="profile"/>
<title>The 50 Best Sandwiches in Chicago – Chicago Magazine</title>
<style type="text/css">
					.heateor_sss_button_instagram span.heateor_sss_svg,a.heateor_sss_instagram span.heateor_sss_svg{background:radial-gradient(circle at 30% 107%,#fdf497 0,#fdf497 5%,#fd5949 45%,#d6249f 60%,#285aeb 90%)}
						div.heateor_sss_horizontal_sharing a.heateor_sss_button_instagram span{background:#000!important;}div.heateor_sss_standard_follow_icons_container a.heateor_sss_button_instagram span{background:#000;}
										.heateor_sss_horizontal_sharing .heateor_sss_svg,.heateor_sss_standard_follow_icons_container .heateor_sss_svg{
							background-color: #000!important;
				background: #000!important;
							color: #fff;
						border-width: 0px;
			border-style: solid;
			border-color: transparent;
		}
					.heateor_sss_horizontal_sharing .heateorSssTCBackground{
				color:#666;
			}
					.heateor_sss_horizontal_sharing span.heateor_sss_svg:hover,.heateor_sss_standard_follow_icons_container span.heateor_sss_svg:hover{
						border-color: transparent;
		}
...
  }
}
</script>
</body>
</html>
728x90
반응형
Comments