标签:python urllib2 爬虫 http post
一、urllib2发送请求import urllib2 url = 'http://www.baidu.com' req = urllib2.Request(url) response = urllib2.urlopen(req) print response.read() print response.geturl() print response.info()
urllib2用一个Request对象,来映射HTTP请求,并将这个请求传入urlopen()中去,返回response对象
Request =>Response http就是基于这种请求/应答机制
response对象,是一个文件对象,可以调用read(),info(),geturl()等方法
response.read() 读取返回的内容
response.info()获取返回header
response.geturl() 获取实际访问的url
urllib2使用相同的接口处理所有的URL头,比如可以这样创建一个ftp请求
req = urllib2.Request(‘ftp://duote.com‘)
二、POST请求
import urllib2 url = "http://www.duote.com/index?php" data = {"softname":"quicktime.exe","size":"18763","md5":"HEN35FLK3WP"} req = urllib2.Request(url,data) response = urllib2.urlopen(req) print response.read()
Request(url,data=None,headers={},orgin_req_host=None,univerifiable=False)
urlopen(url, data=None, timeout=<object object>, cafile=None, capath=None, cadefault=False, context=None)
import urllib import urllib2 url = "http://www.2345.com" data = {'name':'Tom','age':'18','studynum':'002195'} urlvalue = urllib.urlencode(data) print urlvalue r_url = url +'?' +urlvalue data = urllib2.urlopen(r_url)
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:python urllib2 爬虫 http post
原文地址:http://blog.csdn.net/kqq397145531/article/details/46916651