2014-04-05 3 views
3

제발, 도와주세요! 나는 Python3.3이 코드를 사용하고Python3 : urllib.error.HTTPError : 금지 된 HTTP 오류 403 :

:

import urllib.request 
import sys 
Open_Page = urllib.request.urlopen(
     "http://wowcircle.com" 
    ).read().decode().encode('utf-8') 

을 그리고 나는이 걸릴 :

Traceback (most recent call last): 
    File "C:\Users\1\Desktop\WCLauncer\reg.py", line 5, in <module> 
    "http://forum.wowcircle.com" 
    File "C:\Python33\lib\urllib\request.py", line 156, in urlopen 
    return opener.open(url, data, timeout) 
    File "C:\Python33\lib\urllib\request.py", line 475, in open 
    response = meth(req, response) 
    File "C:\Python33\lib\urllib\request.py", line 587, in http_response 
    'http', request, response, code, msg, hdrs) 
    File "C:\Python33\lib\urllib\request.py", line 507, in error 
    result = self._call_chain(*args) 
    File "C:\Python33\lib\urllib\request.py", line 447, in _call_chain 
    result = func(*args) 
    File "C:\Python33\lib\urllib\request.py", line 692, in http_error_302 
    return self.parent.open(new, timeout=req.timeout) 
    File "C:\Python33\lib\urllib\request.py", line 475, in open 
    response = meth(req, response) 
    File "C:\Python33\lib\urllib\request.py", line 587, in http_response 
    'http', request, response, code, msg, hdrs) 
    File "C:\Python33\lib\urllib\request.py", line 507, in error 
    result = self._call_chain(*args) 
    File "C:\Python33\lib\urllib\request.py", line 447, in _call_chain 
    result = func(*args) 
    File "C:\Python33\lib\urllib\request.py", line 692, in http_error_302 
    return self.parent.open(new, timeout=req.timeout) 
    File "C:\Python33\lib\urllib\request.py", line 475, in open 
    response = meth(req, response) 
    File "C:\Python33\lib\urllib\request.py", line 587, in http_response 
    'http', request, response, code, msg, hdrs) 
    File "C:\Python33\lib\urllib\request.py", line 507, in error 
    result = self._call_chain(*args) 
    File "C:\Python33\lib\urllib\request.py", line 447, in _call_chain 
    result = func(*args) 
    File "C:\Python33\lib\urllib\request.py", line 692, in http_error_302 
    return self.parent.open(new, timeout=req.timeout) 
    File "C:\Python33\lib\urllib\request.py", line 475, in open 
    response = meth(req, response) 
    File "C:\Python33\lib\urllib\request.py", line 587, in http_response 
    'http', request, response, code, msg, hdrs) 
    File "C:\Python33\lib\urllib\request.py", line 513, in error 
    return self._call_chain(*args) 
    File "C:\Python33\lib\urllib\request.py", line 447, in _call_chain 
    result = func(*args) 
    File "C:\Python33\lib\urllib\request.py", line 595, in http_error_default 
    raise HTTPError(req.full_url, code, msg, hdrs, fp) 
urllib.error.HTTPError: HTTP Error 403: Forbidden 

이해, 내가 사이트 wowcircle.com에 액세스 할 수 없다. 하지만 난 단지 소스 코드를 가져 가고 싶다! 나는 내가 할 수 있다고 믿는다.

답변

3

헤더를 적절히 설정하는 것이 좋습니다. 브라우저에서 보내는 내용을 확인하십시오 (HTTP 헤더 플러그인).

함수는 다음과 같이 보일 수 있습니다 :

def openAsOpera(url): 
    u = urllib.URLopener() # Python 3: urllib.request.URLOpener 
    u.addheaders = [] 
    u.addheader('User-Agent', 'Opera/9.80 (Windows NT 6.1; WOW64; U; de) Presto/2.10.289 Version/12.01') 
    u.addheader('Accept-Language', 'de-DE,de;q=0.9,en;q=0.8') 
    u.addheader('Accept', 'text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1') 
    f = u.open(url) 
    content = f.read() 
    f.close() 
    return content 

이 기본 버전보다 클라이언트에서 더 기대하는 일부 웹 페이지에 일부 오류가 주위를 가져옵니다. 지금 접근 할 수 있다는 것을 의미

Traceback (most recent call last): 
    File "<pyshell#0>", line 1, in <module> 
    s = openAsOpera('http://wowcircle.com/') 
    File "C:....pyw", line 522, in openAsOpera 
    f = u.open(url) 
    File "C:\Python27\lib\urllib.py", line 208, in open 
    return getattr(self, name)(url) 
    File "C:\Python27\lib\urllib.py", line 359, in open_http 
    return self.http_error(url, fp, errcode, errmsg, headers) 
    File "C:\Python27\lib\urllib.py", line 376, in http_error 
    return self.http_error_default(url, fp, errcode, errmsg, headers) 
    File "C:\Python27\lib\urllib.py", line 381, in http_error_default 
    raise IOError, ('http error', errcode, errmsg, headers) 
IOError: ('http error', 302, 'Moved Temporarily', <httplib.HTTPMessage instance at 0x02C8F1C0>) 

때문에 당신은 실제 브라우저의 가짜 요청 :

지금 나는이 오류가 발생합니다. whis에 다음 http://wowcircle.com/?pmtry=1과 : 리디렉션이 위치로 이동 :

>>> try: s = openAsOpera('http://wowcircle.com/?pmtry=1') 
except: import sys; ty, err, tb = sys.exc_info() 

>>> err.args[3].headers 
['Server: nginx\r\n', 'Date: Sat, 05 Apr 2014 07:42:00 GMT\r\n', 'Content-Type: text/html\r\n', 'Content-Length: 154\r\n', 'Connection: close\r\n', 'Set-Cookie: PMBC=9979187990a58a5bfdaa6d1380ad6156; path=/\r\n', 'Location: http://wowcircle.com/?pmtry=1\r\n'] 

한 thinkg가 주목하는 http://wowcircle.com/?pmtry=2. 카운트 다운. 그리고 쿠키를 기다리는 것 같습니다.

그래서 내 분석의 결과는 다음과 같습니다. 쿠키을 사이트에 액세스 할 때마다 보내는 것을 잊지 마십시오.

+0

난에 AttributeError이 : [U = urllib.URLopener()] 내가 URLLIB을 가져올 수 있지만, 그것은 나를 다시) – Lwt

+0

내가 파이썬 2 코드를 게시하시기 바랍니다 도움을 도움이되지 않습니다. 파이썬 3에서'urllib.request.URLOpener'가 필요하다고 생각합니다. – User