想要使用Python3.4登录top500.org时遇到问题: urllib.error.HTTPError:HTTP错误403:FORBIDDEN ,当Py3尝试执行时 下面的代码中的resp = urllib.request.urlopen(req)字符串:
但是在登录其他网站时我没有任何错误!
import http.client
import http.cookiejar
import urllib.request
authentication_url = 'http://top_.org/accounts/login/'
payload = {
'username': 'username',
'password': '*******'
}
cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
opener.addheaders = [('User-agent',
('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10) '
'AppleWebKit/535.1 (KHTML, like Gecko) '
'Chrome/13.0.782.13 Safari/535.1'))
]
opener.addheaders = [('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')]
opener.addheaders = [('Accept-Language', 'en-EN')]
opener.addheaders = [('Referrer', authentication_url)]
opener.addheaders = [('Accept-Charset', 'UTF-8')]
urllib.request.install_opener(opener)
data = urllib.parse.urlencode(payload)
binary_data = data.encode('UTF-8')
req = urllib.request.Request(authentication_url, binary_data)
resp = urllib.request.urlopen(req)
contents = resp.read()
print(contents)