python中requests库使用方法详解

1、标准的restful接口调用
    (1)requests.post方法调三方接口(用的是data)
    r = requests.post(url + 'company/add_friend/', data={'id': zid, 'com_key': com_key})
    # 这一步将返回值转成json
    key = json.loads(r.text)

    (2)requests.get方法调三方接口(用的是params)
    r = requests.get(url + 'company/search_user/', params={'id': id, 'pub_key': pub_key})
    # 这一步将返回值转成json
    key = json.loads(r.text)

2、模拟浏览器请求加上headers参数,只写了requests.post(),get方式自行百度
   headers部分,F12 -> Network -> Request Header -> (参数全部复制过来)
    # -*- coding: utf-8 -*
    import requests
    import json

    def get_translate_date(word =None):

        url ='https://www.versio.nl/assets/ajax/DomainAvailibility/LoadAvailableDomains'

        domain = word

        form_data = {
            'domain': domain,
            'multi': 'false'
        }

        request_headers = {
            'Accept': 'application/json, text/javascript, */*; q=0.01',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
            'Connection': 'keep-alive',
            'Content-Length': '25',
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Cookie': '_ga=GA1.2.302656271.1572859335; _gid=GA1.2.1059880577.1572859335; intercom-id-bqq80vdz=61a7d332-44d3-49d8-998c-c109e17e6f0e; _gcl_au=1.1.1255287957.1572914640; _hjid=e636c649-dd67-43b6-84de-7c84d844fc12; cookie_accepted=1; PHPSESSID=vf47dt0scgb1mbmpid1n2pi7p7; _hjIncludedInSample=1; _fbp=fb.1.1573020598509.1791592550',
            'Host': 'www.versio.nl',
            'Origin': 'https://www.versio.nl',
            'Referer': 'https://www.versio.nl/domeinnamen',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Site': 'same-origin',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36',
            'X-Requested-With': 'XMLHttpRequest'
        }

        # 请求表单数据
        response = requests.post(url, data=form_data, headers=request_headers)

        # 将JSON格式字符串转字典
        # content = json.loads(response.text)
        content = response.text
        # 打印翻译后的数据
        print(content)
        print(response.text)
        print(response.status_code)
        # print(response.headers)


    if __name__ == '__main__':
        get_translate_date("sky.nl")

3、requests.Session(),先获取一下session,再携带session,访问接口

import json

import requests

# 通过Session类新建一个会话
session = requests.Session()
post_url = 'https://www.versio.nl/domeinnamen'
# 往下使用requests的地方,直接使用session即可,session就会保存服务器发送过来的cookie信息
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36',
    # 'Referer': 'https://passport.weibo.cn/signin/login?entry=mweibo&r=http%3A%2F%2Fweibo.cn%2F&backTitle=%CE%A2%B2%A9&vt=',
}
data = {
    # 'username': '17312345678', # 账号
}

r = session.get(url=post_url, headers=headers)
# print(r.cookies)
# print(json.dumps(r.cookies['cookies']))
# params=data
# 上面的session会保存会话,往下发送请求,直接使用session即可


url = 'https://www.versio.nl/assets/ajax/DomainAvailibility/LoadAvailableDomains'
# url = 'https://www.versio.nl/assets/ajax/DomainAvailibility/CheckAvailibilityDomains'

querystring = {"domain": "liming.nl", "multi": "false"}
headers1 = {
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
    'Connection': 'keep-alive',
    'Content-Length': '25',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    # 不要再传session值了
    # 'Cookie': '_ga=GA1.2.302656271.1572859335; _gid=GA1.2.1059880577.1572859335; intercom-id-bqq80vdz=61a7d332-44d3-49d8-998c-c109e17e6f0e; _gcl_au=1.1.1255287957.1572914640; _hjid=e636c649-dd67-43b6-84de-7c84d844fc12; cookie_accepted=1; PHPSESSID=vf47dt0scgb1mbmpid1n2pi7p7; _hjIncludedInSample=1; _fbp=fb.1.1573020598509.1791592550',
    'Host': 'www.versio.nl',
    'Origin': 'https://www.versio.nl',
    'Referer': 'https://www.versio.nl/domeinnamen',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest'
}

response = session.post(url, headers=headers1, data=querystring)
print(response.text)
print(response.status_code)




 

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页