996技术站 - 活在未来 | KingSun966技术站,极客带你看世界!
https://www.996station.com程序员开发指南Description
https://guide.996station.com
requests 模块
requests 安装
- 安装
pip install requests
requests 基本使用
#!/usr/bin/python
# -*- coding: utf-8 -*-
# 1. 导入模块
import requests
# 2. 请求数据获取响应
url = 'http://www.baidu.com'
response = requests.get(url)
# 3. 处理响应内容
# 获取二进制内容
# print(response.content)
# 获取字符串内容,默认字符串编码 utf-8,也可以在获取之前字符串之前设置
response.encoding = 'utf-8'
print(response.text)
requests 自定头
#!/usr/bin/python
# -*- coding: utf-8 -*-
# 1. 导入模块
import requests
# 2. 请求数据获取响应
url = 'http://www.baidu.com/s'
# 定义请求头
headers = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.170 Safari/537.36'
}
# 定义 GET 请求参数
wd = input("请输入查询内容:")
params = {
"wd": wd
}
response = requests.get(url,params=params)
# 3. 处理响应内容
# 获取二进制内容
# print(response.content)
# 获取字符串内容,默认字符串编码 utf-8,也可以在获取之前字符串之前设置
response.encoding = 'utf-8'
print(response.text)
requests 其他方法
#!/usr/bin/python
# -*- coding: utf-8 -*-
# 1. 导入模块
import requests
# 2. 请求数据获取响应
url = 'http://baidu.com/s'
# 定义请求头
headers = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.170 Safari/537.36'
}
# 定义 GET 请求参数
wd = input("请输入查询内容:")
params = {
"wd": wd
}
response = requests.get(url,params=params,headers=headers)
# 响应状态码
print(response.status_code)
# 响应头信息
print(response.headers)
# 响应后的url地址
print(response.url)
# 获取请求头
print(response.request.headers)
# 3. 处理响应内容
# 获取二进制内容
# print(response.content)
# 获取字符串内容,默认字符串编码 utf-8,也可以在获取之前字符串之前设置
# response.encoding = 'utf-8'
# print(response.text)
requests 获取资源数据
#!/usr/bin/python
# -*- coding: utf-8 -*-
# 1. 导入模块
import requests
# 2. 请求数据获取响应
url = 'http://docs.python-requests.org/zh_CN/latest/_static/requests-sidebar.png'
# 定义请求头
headers = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.170 Safari/537.36'
}
response = requests.get(url)
# 保存资源内容
with open('test.png','wb') as f:
f.write(response.content)
requests 分析获取ajax数据
#!/usr/bin/python
# -*- coding: utf-8 -*-
# 1. 导入模块
import requests
# 2. 请求数据获取响应
url = 'https://movie.douban.com/j/search_subjects'
params = {
"type":"tv",
"tag":"热门",
"sort":"recommend",
"page_limit":20,
"page_start":0
}
# 定义请求头
headers = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.170 Safari/537.36',
'Referer':"https://movie.douban.com/tv/",
'X-Requested-With':'XMLHttpRequest'
}
response = requests.get(url,headers=headers,params=params)
# 保存资源内容
with open('sample05.json','wb') as f:
f.write(response.content)