requests补充

HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS、GET、HEAD、POST、PUT、DELETE、TRACE、CONNECT 这几种。其中,POST 一般用来向服务端提交数据,本文主要讨论 POST 提交数据的几种方式。

这里主要讨论一下requests模仿浏览器请求的四种方式。

一、application/x-www-form-urlencoded

这种方式的传递参数,在requests中,属于get的方法。会有一个字典形式的数据,然后我们在请求的时候:

import requests

url = 'https://i.cnblogs.com/EditPosts.aspx?opt=1'
parameter = {
    '123': '456',
    '234': '345'
}
r = requests.get(url, params=parameter)

这种请求方式比较常见,一般学requests的时候都会用到。

二、multipart/x-www-form-data

这种属于post中上传文件的方式,具体代码:

url = 'https://i.cnblogs.com/EditPosts.aspx?opt=1'
files = {'app_id': (None, '123456'),
         'version': (None, '2256'),
         'platform': (None, 'ios'),
         'libzip': ('libmsc.zip', open('C:Usersdanwang3Desktoplibmsc.zip', 'rb'), 'application/x-zip-compressed')
 }
response = requests.post(url, files=files)

# 代码是从http://blog.csdn.net/j_akill/article/details/43560293,拷贝来的,没有用过。

这种请求方式,在做爬虫的时候很少会遇见,如果有需求,requests也是可以实现这样的方式进行post提交的。

三、application/json

这种方式和第一种的使用率是一样的,都很高,也得益于json格式的流行。很常见的post请求数据格式。

import requests

url = 'https://i.cnblogs.com/EditPosts.aspx?opt=1'
form_data = {
    '123': '456',
    '234': '345'
}
r = requests.get(url, data=form_data)

这种请求方式,代码也比较简洁,很常见。

它还有一种变种方式:request payload,这种是浏览器发送的ajax请求

{
number: "18047137483",
password: "",
randomPass: "",
sRand: "SSOLogin",
userLoginType: "4"
}

它的不同之处,是长得像字典,但是仔细一看不是字典,如果你写成字典,请求就会失败。

我们在模拟这样的请求的时候,需要使用到json

import json
import requests

url = 'http://www.***'
data = {
    '123': '654',
    'abc': 'ddd',
}
form_data = json.dumps(data)
r = requests.post(url, data=form_data)
html = r.text
# 在json.dumps之前,我们还是要一个字典形式的数据的。

 另外还有一种,也是request payload的数据类型。是目前见过的最变态的请求数据格式:

callCount=1
page=/service/bill/customerbill/index.jsp?bill=detail
httpSessionId=
scriptSessionId=AA0CBE9FB90164F9E0E55CF74FCC9338568
c0-scriptName=Service
c0-methodName=excute
c0-id=0
c0-param0=string:TWB_GET_MONTH_DETAIL_BILL_NEW
c0-param1=boolean:false
c0-e1=string:myPage
c0-e2=string:myPage_table
c0-e3=string:TWB_GET_MONTH_DETAIL_BILL_NEW
c0-e4=boolean:false
c0-e5=string:15
c0-e6=string:1
c0-e7=null:null
c0-e8=boolean:false
c0-e9=null:null
c0-e10=string:-1
c0-param2=Object_Object:{div_id:reference:c0-e1, table_id:reference:c0-e2, func_id:reference:c0-e3, is_sql:reference:c0-e4, 
page_size:reference:c0-e5, page_index:reference:c0-e6, exp_excel:reference:c0-e7, hide_pager:reference:c0-e8, class_name:reference:c0-e9,
area_code:reference:c0-e10} batchId=4 # 以上是请求的时候页面上显示的部分数据

这种数据我们怎么去post呢?当然是有办法的,不止一种,这里举一个实用的例子

import requests

url = 'http://****'
form_data = {
        'callCount': '1',
        'page': '/service/bill/customerbill/index.jsp?bill=balance',
        'httpSessionId': '',
        'scriptSessionId': 'AA0CBE9FB90164F9E0E55CF74FCC9338775',
        'c0-scriptName': 'Service',
        'c0-methodName': 'excute',
        'c0-id': '0',
        'c0-param0': 'string:TWB_GET_MONTH_DETAIL_BILL',
        'c0-param1': 'boolean:false',
        'c0-e1': 'string:myPage',
        'c0-e2': 'string:myPage_table',
        'c0-e3': 'string:TWB_GET_MONTH_DETAIL_BILL',
        'c0-e4': 'boolean:false',
        'c0-e5': 'string:15',
        'c0-e6': 'string:1',
        'c0-e7': 'null:null',
        'c0-e8': 'boolean:false',
        'c0-e9': 'null:null',
        'c0-e10': 'string:-1',
        'c0-param2': 'Object_Object:{div_id:reference:c0-e1, table_id:reference:c0-e2, func_id:reference:c0-e3, is_sql:reference:c0-e4,
page_size:reference:c0-e5, page_index:reference:c0-e6, exp_excel:reference:c0-e7, hide_pager:reference:c0-e8, class_name:reference:c0-e9,
area_code:reference:c0-e10,}', 'batchId': '4' }. r = requests.post(url, data=form_data) # 因为是演示请求,数据可能和原始数据不太对应,这里只是伪代码,明白意思即可。

  

四、text/xml

这种请求方式,是不才最近刚遇到的一种方式。之前也没有接触过,它是使用post请求方式给服务器端发送了一个带HTML标签的字符串。这种在编写的时候也比较简单,但是因为不常见,所以也困扰了比较长的时间。

url = 'https://i.cnblogs.com/EditPosts.aspx?opt=1'
payload = '''
<buffalo-call>
<method>getCode</method>
<map>
<type>java.util.HashMap</type>
<string>ABC</string>
<string>123654</string>
<stringID</string>
</map>

</buffalo-call>
'''
headers = {
    'Content-Type': 'text/xml;charset=UTF-8'
}
r = requests.post(url, data=payload, headers=headers)
# 注:在遇到这种数据方式的时候,我们一定要对请求头的'Content-Type'进行设置,让它的值为'text/xml'

  

因为这种形式的请求很少见,所以难免会造成一些困惑,记下来以便后续查询,也希望能帮助刚接触requests的小伙伴。

原文地址:https://www.cnblogs.com/sxzwj/p/6857404.html