2016-11-06 6 views
0

파이썬 3.5의 http://learncodethehardway.org/words.txt URL에 액세스하려고하는데 아래 코드를 시도했지만 403 HTTP 오류가 발생했습니다. 어떻게 이것을 피하려면 headers을 구성해야합니까?요청에 금지 된 (403) 오류, Python 3.5의 urlopen

import urllib.request 
url = "http://learncodethehardway.org/words.txt" 

hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11', 
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 
    'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 
    'Accept-Encoding': 'none', 
    'Accept-Language': 'en-US,en;q=0.8', 
    'Connection': 'keep-alive'} 
req = urllib.request.Request(url, None, headers=hdr) 
html = urllib.request.urlopen(req) 

print (html.read()) 
+0

가 왜 403 오류가 발생한다? –

+0

코드를 그대로 실행했는데 403없이 응답을 받았습니다. –

답변

0

이 나를 위해 작동합니다

자세한 내용은
import urllib.request 
url = 'http://learncodethehardway.org/words.txt' 
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'}) 
html = urllib.request.urlopen(req).read() 
print(html.decode()) 

은 비슷한 질문을 참조하십시오 HTTP error 403 in Python 3 Web Scraping

+0

다른 사용자를 위해 링크의 주요 세부 정보를 게시물에 추가하도록 편집하십시오. –