# 引入Requests库
import requests
# 发起GET请求
response = requests.get('https://www.baidu.com/')
# 查看响应类型 requests.models.Response
print(type(response))
# 输出状态码
print(response.status_code)
# 输出响应内容类型 text
print(type(response.text))
# 输出响应内容
print(response.text)
# 输出cookies
print(response.cookies)
#带参数的GET请求
import requests
# 分装GET请求参数
param = {'name':'xxx','age':xxx}
# 设置GET请求参数(Params)
response = requests.get('http://xxxxx,params=param)
print(response.text)
#json格式
response = requests.get('http://www.baidu,com')
# 获取响应内容
print(type(response.text))
# 如果响应内容是json,就将其转为json
print(response.json())
# 输出的是字典类型
print(type(response.json()))
#添加headers
import requests
# 设置User-Agent浏览器信息
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"
}
# 设置请求头信息
response = requests.get('https://www.zhihu.com/explore',headers=headers)
print(response.text)