python爬虫之urllib
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python爬虫之urllib相关的知识,希望对你有一定的参考价值。
#POST import urllib import urllib2 values = {} values[‘username‘] = "[email protected]" values[‘password‘] = "XXXX" data = urllib.urlencode(values) url = "http://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn" request = urllib2.Request(url,data) response = urllib2.urlopen(request) print response.read() #GET import urllib import urllib2 values={} values[‘username‘] = "[email protected]" values[‘password‘]="XXXX" data = urllib.urlencode(values) url = "http://passport.csdn.net/account/login" geturl = url + "?"+data request = urllib2.Request(geturl) response = urllib2.urlopen(request) print response.read() #headers import urllib import urllib2 url = ‘http://www.server.com/login‘ user_agent = ‘Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)‘ values = {‘username‘ : ‘cqc‘, ‘password‘ : ‘XXXX‘ } headers = { ‘User-Agent‘ : user_agent } data = urllib.urlencode(values) request = urllib2.Request(url, data, headers) response = urllib2.urlopen(request) page = response.read()
以上是关于python爬虫之urllib的主要内容,如果未能解决你的问题,请参考以下文章