Skip to content

Instantly share code, notes, and snippets.

@muzizongheng
Created July 18, 2013 06:01
Show Gist options
  • Save muzizongheng/6027009 to your computer and use it in GitHub Desktop.
Save muzizongheng/6027009 to your computer and use it in GitHub Desktop.
create http get using cookie
#install cookie
cj = cookiejar.CookieJar();
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj));
urllib.request.install_opener(opener)
#build request for accessed url
homeReq = urllib.request.Request(
url = csdnAccessModuleUrl
)
homeReq.add_header('Accept', 'text/html, application/xhtml+xml, */*');
homeReq.add_header('Accept-Language', 'en-US')
homeReq.add_header('Accept-Encoding', 'gzip, deflate')
homeReq.add_header('Connection', 'Keep-Alive');
homeReq.add_header('Referer', 'http://passport.csdn.net/account/login?from='+urllib.parse.quote(moduleUrl))#http%3a%2f%2fwrite.blog.csdn.net%2f')
homeReq.add_header('User-Agent', 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)');
#open access url
resp = urllib.request.urlopen(homeReq)
print(resp.info())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment