Request模块(get请求)篇

- HTTP for Humans,更简洁更友好
- 继承了urllib的所有特征
- 底层使用的是urllib3
- 开源地址: https://github.com/requests/requests
- 中文文档: http://docs.python-requests.org/zh_CN/latest/index.html
- 安装: conda install requests

- get请求
- requests.get(url)
- requests.request("get", url)
- 可以带有headers和parmas参数

 案例一:

import requests

url = "http://www.baidu.com"

# 两种请求方式
# 使用get请求
rsp = requests.get(url)
print(rsp.text)


# 使用request请求
rsp = requests.request("get",url)
print(rsp.text)

运行结果如下:

案例二:


'''
使用参数headers和params
研究返回结果

'''

import requests

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

kw = {
"wd": "好人"
}

headers = {
"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
}

rsp = requests.get(url,params=kw,headers=headers)

# print(rsp.text)
# print(rsp.content)
print(rsp.url)
print(rsp.encoding)
print(rsp.status_code)

运行结果如下:

猜你喜欢

转载自www.cnblogs.com/jerryspace/p/9851361.html