Python-随请求发送data/headers

来源:互联网 发布:排序算法比较次数 编辑:程序博客网 时间:2024/05/23 11:56

version:2.7.12

#coding=utf-8import urllibimport urllib2#url = 'http://www.baidu.com'url = 'https://hao.360.cn/?wd_xp1'values = {'name' : 'WHY',          'location' : 'SDU',          'language' : 'Python' }send_headers = {#'Host':'http://www.baidu.com',                #'Host':'https://hao.360.cn/?wd_xp1',                'User-Agent':'Mozilla/5.0 (Windows NT 6.2; rv:16.0) Gecko/20100101 Firefox/16.0',                'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',                'Connection':'keep-alive'}data = urllib.urlencode(values,'utf8') # 编码工作dataHeaders = urllib.urlencode(send_headers,'utf8')#req = urllib2.Request(url)#req = urllib2.Request(url = url,headers = send_headers)  # 发送请求同时传【headers】【post】#req = urllib2.Request(url+'?'+dataHeaders)  # 发送请求同时传【headers】【get】#req = urllib2.Request(url+'?'+data)  # 发送请求同时传【data】【get方式】#req = urllib2.Request(url,data)  # 发送请求同时传【data】【post方式】【百度不可用,360可用】req = urllib2.Request(url,data,send_headers)  # 发送请求同时传【data+headers】【post方式】response = urllib2.urlopen(req)  #接受反馈的信息print response.info()#打印头部信息print "\"real path\" :",response.geturl()the_page = response.read()  #读取反馈的内容print the_page.decode('UTF8')
0 0
原创粉丝点击