python requests 设置headers 和 post请求体x-www-form-urlencoded

1.application/json:是JSON格式提交的一种识别方式。在请求头里标示。
2.application/x-www-form-urlencoded : 这是form表单提交的时候的表示方式。
比如我们ajax提交,如果dataType是json,那么请求头就是application/json,而我们平常的form提交那么就是application/x-www-form-urlencoded,自己浏览器控制台看看就知道了。
3.multipart/form-data
这又是一个常见的 POST 数据提交的方式。我们使用表单上传文件时,必须让 form 的 enctyped 等于这个值
4.text/xml
它是一种使用 HTTP 作为传输协议,XML 作为编码方式的远程调用规范

#coding:utf-8
from fake_useragent import UserAgent;
import requests
ua=UserAgent()
headers={
    "Proxy-Connection": "keep-alive",
    "Pragma": "no-cache",
    # "DNT":"1",
    "User-Agent":ua.random,
    "Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4",
    "Referer": "www.huixiaoer.com",
    "Accept-Charset": "gb2312,gbk;q=0.7,utf-8;q=0.7,*;q=0.7",
    "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
    "Accept-Encoding":"gzip, deflate, sdch",
    "Cache-Control":"max-age=0",
    "Connection":"keep-alive",
    "Content-Type":"application/x-www-form-urlencoded",
    # "Cookie":"pgv_pvi=9755294720; aliyungf_tc=AQAAAIEbHSUhVA4ATkxVeHH7o+UfJUCq; acw_tc=AQAAAFesTD4BeQ4ATkxVeNrQ4zX/wI03; PHPSESSID=flc3hhtdbcgvr4pgekhvk7rrb1; pgv_si=s7094364160; city=440100; _ga=GA1.2.968334469.1525633526; _gid=GA1.2.1982983394.1525633526; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22162fc16d967476-00a0ee49af4a81-5d4e211f-1049088-162fc16d9684e5%22%2C%22%24device_id%22%3A%22162fc16d967476-00a0ee49af4a81-5d4e211f-1049088-162fc16d9684e5%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_utm_source%22%3A%22bdpp_web%22%2C%22%24latest_utm_medium%22%3A%22ppc%22%2C%22%24latest_utm_campaign%22%3A%22title_20180223%22%7D%7D; Hm_lvt_d47d0c2743e9b14d07c86e077d6bdaa2=1524647779,1524651322,1525633526,1525673504; Hm_lpvt_d47d0c2743e9b14d07c86e077d6bdaa2=1525674666",
    "Host":"www.huixiaoer.com",
    "Upgrade-Insecure-Requests":"1",
    "X-Requested-With":"XMLHttpRequest"
}



cookies={
        "pgv_pvi":"9755294720",
        "aliyungf_tc":"AQAAAIEbHSUhVA4ATkxVeHH7o+UfJUCq",
        "acw_tc":"AQAAAFesTD4BeQ4ATkxVeNrQ4zX/wI03",
        "PHPSESSID":"flc3hhtdbcgvr4pgekhvk7rrb1",
        "pgv_si":"s7094364160",
        "city":"440100",
        "_ga":"GA1.2.968334469.1525633526",
        "_gid":"GA1.2.1982983394.1525633526",
        "sensorsdata2015jssdkcross":"%7B%22distinct_id%22%3A%22162fc16d967476-00a0ee49af4a81-5d4e211f-1049088-162fc16d9684e5%22%2C%22%24device_id%22%3A%22162fc16d967476-00a0ee49af4a81-5d4e211f-1049088-162fc16d9684e5%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_utm_source%22%3A%22bdpp_web%22%2C%22%24latest_utm_medium%22%3A%22ppc%22%2C%22%24latest_utm_campaign%22%3A%22title_20180223%22%7D%7D",
        "Hm_lvt_d47d0c2743e9b14d07c86e077d6bdaa2":"1524647779,1524651322,1525633526,1525673504",
        "Hm_lpvt_d47d0c2743e9b14d07c86e077d6bdaa2":"1525674666"
}

datas={
    'conditions':'{"city_code":"440100","hid":-1,"capacity":-1,"type_code":-1,"tag":-1,"keyword":-1,"key":-1,"lat":"0","lng":"0","center_name":-1,"has_package":-1,"has_special":0,"has_conference":1,"order":-1,"dur":-1,"bud":-1,"page":1,"num":"","cap_num":"","qt":0}'
}

url="http://www.huixiaoer.com/so-api/ajax-get-so-data"
session=requests.session()
requ=session.post(url,data=datas,headers=headers,cookies=cookies)
res=requ.text
print(res)

猜你喜欢

转载自www.cnblogs.com/wtcl/p/9006709.html