首页 > 其他分享 >requests基本使用

requests基本使用

时间:2023-02-10 11:11:15浏览次数:36  
标签:基本 baidu url text headers 使用 requests response

requests基本使用

​ 官方文档:http://cn.python‐requests.org/zh_CN/latest/

​ 快速上手:http://cn.python‐requests.org/zh_CN/latest/user/quickstart.html

​ 安装:pip install requests

import requests

url = 'https://www.baidu.com'
# 发送请求
response = requests.get(url=url)

print(type(response))
# 设置响应编码
response.encoding = 'utf-8'
# 获取字符串格式网页源码
text = response.text

print(text)
# 获取请求 url
request_url = response.url

print(request_url)
# 获取二进制格式网页源码
content = response.content

print(content)
# 获取响应状态码
status_code = response.status_code

print(status_code)
# 获取响应头
headers = response.headers

print(headers)

​ 发送 get 请求,eg:

import requests

url = 'https://www.baidu.com/s'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
    'cookie':'BIDUPSID=DE8BD592DC443ACE32C8656F79232585; PSTM=1668828319; BAIDUID=1A7F77EE719BE5FF1E947F8ADE894490:FG=1; BD_UPN=12314753; BDUSS=TNUUW1NMlZYMFA1eHcxSjk4Z2pUUFFBWUx2bld-TmhtNHp0T2dBWEN5TWw1TEpqSVFBQUFBJCQAAAAAAAAAAAEAAAAApdijuNbM-taxxNCwose~AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACVXi2MlV4tjS; BDUSS_BFESS=TNUUW1NMlZYMFA1eHcxSjk4Z2pUUFFBWUx2bld-TmhtNHp0T2dBWEN5TWw1TEpqSVFBQUFBJCQAAAAAAAAAAAEAAAAApdijuNbM-taxxNCwose~AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACVXi2MlV4tjS; MCITY=-257:; ZFY=YqPVXB55b:AXm9Z:B68HdwlAxrOdtqrH1OlZ5fmRHPG80:C; BAIDUID_BFESS=1A7F77EE719BE5FF1E947F8ADE894490:FG=1; __bid_n=184982b230755690e44207; RT="z=1&dm=baidu.com&si=xyk5608w5h&ss=ldfn1bpm&sl=2&tt=o0y&bcn=https://fclog.baidu.com/log/weirwood?type=perf&ld=rui&ul=1zbn&hd=1zd0"; BD_HOME=1; BA_HECTOR=ak050k858h80a4ag2k01ahgk1htfdjt1k; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; BD_CK_SAM=1; PSINO=6; delPer=0; H_PS_PSSID=37779_36543_38094_37907_37989_37796_37931_38089_37900_26350_38008_37881; H_PS_645EC=f412xaOfJuznHYk9hJOLutqg1UaiPDbjRfXL8p6PwSDteU4YWkRjKYnxRo8tb3g7IQ6G; BDSVRTM=323; baikeVisitId=11af4715-f677-4ddb-a7f2-c296a2bb8d08; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598'
}

params = {
    'wd': '北京'
}
response = requests.get(url=url, params=params, headers=headers)
text = response.text
print(text)

​ 跟 urllib 的不同之处:通过 params 传递参数;参数不需要 urlencode 编码;不需要请求对象的定制;url 中的 ?可以省略

​ 发送 post 请求(百度翻译),eg:

import json

import requests

url = 'https://fanyi.baidu.com/v2transapi?from=en&to=zh'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
    'Cookie': 'BIDUPSID=FE844A67B2FF56C5ADABC85593863D5C; PSTM=1667189167; BAIDUID=6D23EF3A381CB93BF5C068B9D4FDEA14:FG=1; APPGUIDE_10_0_2=1; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BDSFRCVID=avtOJeC627YLYa3jGtkwhhHrzKoEzoJTH6ao5T1ROv0CoasKvifEEG0PXU8g0KFM3JSsogKK3gOTH4DF_2uxOjjg8UtVJeC6EG0Ptf8g0f5; H_B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8pcNLTDKetAuLG59afbJ25T3Bpn-Mq6BDn5GXpO1j4_eWlOvBCrBQeOfXq6cbJTtsq5jDh3B25ksD-RlW6JZL57y0hvctb3cShPmQMjrDRLbXU6BK5vPbNcZ0l8K3l02V-bIe-t2b6QhDNAHq6kHJbksL-35HJj2jJQv2KTjh6P_5lO9BtQmJJufhlRwKCO8sPbc5P7sybDhyUn4htRZQg-q3RAaQIjC8PLxLPnHKUPBMfj40x-jLNbPVn0MWhQYhpcbKPnJyUnybPnnBT3R3H8HL4nv2JcJbM5m3x6qLTKkQN3T-PKO5bRh_CcJ-J8XhC-mjjQP; RT="z=1&dm=baidu.com&si=rb78ivac9kn&ss=ldh3elpq&sl=4&tt=3ib&bcn=https://fclog.baidu.com/log/weirwood?type=perf&ld=qiv&ul=1hb0&hd=1hc3"; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; __bid_n=18600716d5eb07b0fc4207; FPTOKEN=Qr60V8dnjuD2DPzWDXeYh0CjqLJ5OgcteW8BcQF+DD+ioi58L59LyzV9Pv9rm3AmF0WNJTVt3qq7nRtvyT0Utg45WbiWiYuNiI59n/uQ2/vcMEeYXYm6eXVamqCckBOEZ+sDloY6bNRHT8q5goSD0ov5aecPdggTGE4nOdfvHvQI88hn3v7ZqwOsjmnDnWW81RTDODiOve/8yYCfbSBebaV/7rArHkRHU+iXkIG19RjgpMfE2wvjmCjV9jwAkJTdJEaNjmEMsTdLLCgkqAOfMmSy/5YL2zGIW6g4W0fmtpqZ62o2TmTsip5j9xMGbCAtXCn+26SuBicKjY7QppaBajJj9SA15JIAUb83Bls9zWQjnH7nAxb8xllG5VGymnpQpEEr1L13bKw38tRkg157iQ==|HZKpjWaOMBcGPsBjdArFZshaEF2iGCe632vZOzZTsH0=|10|f621896e59f25f5099a091229e3cc7fa; BA_HECTOR=81a1242ka10l0h2001042ks61htev161k; H_PS_PSSID=36558_37518_38106_38091_37139_37906_36802_37930_37900_26350_37959_38009_37881; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1675082149; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1675082160; ab_sr=1.0.1_NjQ0MjU1Njg2YjVhNWM2ZmZkYWI2ZjQ3MWNlMGUzMDdlNWJmNWIxZWU1ZjJkN2FjOWFmZThiYWI1MmVmZTkxMjFhZjJkOWQxZjFmOThmMzJiZDlkZmYzMTIyZTU1M2Q1Yjg2ZTc3NDMzMjVhYzExODEzMzQzYTE5M2FjNmFjMzVkOThmODMyNDI1YjkxMmU5NWMxYTY0MWE2ZmMzYzk3NmIxNjBkMjQ3ZmMxMGQ1NTI5Mjg3NWE1ODY3MGVkNWI3',
    'acs-token': '1675065925630_1675082160077_YsOCM9dQF8Rk2CtfXhsne4RzXxT08iQIQVhe5wEe2XhlfcVUz76D6HUp+W7pAajL+Ii/80NVSlhaaDJ7AmswIQwxJ+pUu6QnIeqojfqptumTXPQvRjcQbY1XqBzrgIiGf/O7TN6oPgBwpbxSWblcDJZg/PJlCPI/2onug1PMCbC/rM7PGkc1Iz62hf6EePshxhFtmrGT9edJ+e6iK/Y9ZAdoXjf3jrUjse1CB/1thlJOYBszxykhpgPUGNiopGQtpzqYD6nP7Q6NppTY7VfjISDNWNin1x50nw8HEDRgSdBotLxrRSiIp4CuLJdCc0HJ0E0JWJJKF23l/8YcGF7zuw=='
}

data = {
    'from': 'en',
    'to': 'zh',
    'query': 'eye',
    'simple_means_flag': '3',
    'sign': '67056.386753',
    'token': '69f8f800722d4702e68dc07ddecc2054',
    'domain': 'common'
}

response = requests.post(url=url, data=data, headers=headers)
json_text = json.loads(response.text)
print(json_text)

​ 代理

import requests

url = 'http://www.baidu.com/s?'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'
}

param = {
    'wd': 'ip'
}

proxy = {
    'http': '121.230.210.31:3256'
}

response = requests.get(url=url, param=param, headers=headers, proxies=proxy)

标签:基本,baidu,url,text,headers,使用,requests,response
From: https://www.cnblogs.com/Y-wee/p/17108250.html

相关文章