一、使用的工具
Python --- Download Python | Python.org
https://www.python.org/getit/
二、安装过程和使用方法教程
三、安装requests工具
1.打开PyCharm工具,创建一个.py后缀的文件,要记住这个文件的地址
2.以管理员身份打开命令提示符其下输入命令: --- 黄色背景是需要敲的代码
PS E:\pyCharm\code> python -m venv venv
PS E:\pyCharm\code> .\venv\Scripts\Activate.ps1
(venv) PS E:\pyCharm\code> python -m pip install requests -i https://pypi.tuna.tsinghua.edu.cn/simple
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting requests
Using cached requests-2.34.2-py3-none-any.whl (73 kB)
Collecting charset_normalizer<4,>=2 (from requests)
Using cached charset_normalizer-3.5.3-cp315-cp315-win_amd64.whl (218 kB)
Collecting idna<4,>=2.5 (from requests)
Using cached idna-3.20-py3-none-any.whl (69 kB)
Collecting urllib3<3,>=1.26 (from requests)
Using cached urllib3-2.8.0-py3-none-any.whl (135 kB)
Collecting certifi>=2023.5.7 (from requests)
Using cached certifi-2026.11.20-py3-none-any.whl (127 kB)
Installing collected packages: urllib3, idna, charset_normalizer, certifi, requests
Successfully installed certifi-2026.11.20 charset_normalizer-3.5.3 idna-3.20 requests-2.34.2 urllib3-2.8.0
notice A new release of pip is available: 26.2.2 -> 26.3
notice To update, run: python.exe -m pip install --upgrade pip
(venv) PS E:\pyCharm\code> .\venv\Scripts\python.exe -c "import requests;print('ok')"
ok
(venv) PS E:\pyCharm\code> pip list
Package Version
certifi 2026.11.20
charset-normalizer 3.5.3
idna 3.20
pip 26.2.2
requests 2.34.2
urllib3 2.8.0
(venv) PS E:\pyCharm\code>
---- #显示这个说明安装成功
四、response的常用属性
r.text 获取字符串形式的网页源码
r.encoding 访问或限定编码格式
r.url 获取请求的最终url
r.content 获取响应的字节类型(即二进制格式的网页源码)
r.status_code 获取响应的状态码
200 --- 请求成功并返回所有资源。
404 --- 请求的资源不存在。
500 --- 服务内部错误
五、实例
5.1get请求
5.1.1 B
#代码如下:
#B站 import requests #1.定义一个URL url = 'http://www.bilibili.com/' #2.模拟浏览器发送请求 requests.get() #创建一个请求头字典 --- 方法请往后翻有截图步骤 #注意:每个主机的请求头值的数据都不一样,一定要用自己的 headers = { 'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/158.0.0.0 Safari/537.36 Edg/158.0.0.0' } responses = requests.get(url,headers=headers) #3.下载保存到本地 with open('bilibili.html','w',encoding='utf-8') as fp: fp.write(responses.text)
#运行结果:
#打开终端
python .\test.py
#一旦运行后左上角目录会多出一个bilibili.html文件
#打开bilibili.html文件

#获取请求头的数据步骤截图


5.1.2知乎
#知乎 import requests #1.定义一个url url = 'https://www.zhihu.com/question/2099419153664947132/answer/2101250259691348163' #2.模拟浏览器发送请求 #创建一个请求头字典 headers = { 'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/158.0.0.0 Safari/537.36 Edg/158.0.0.0' } response = requests.get(url,headers=headers) #3.下载保存到本地 with open('zhihu.html','w')as fp: fp.write(response.text)
#运行结果:
#打开终端
python .\test2.py
#一旦运行后左上角目录会多出一个zhihu.html文件


5.1.3带有参数的get请求
#带有参数的get请求 import requests name = input('请输入你要查看的明星名称:') #定义一个url url = 'http://cn.bing.com/search' #模拟浏览器发起请求 headers = { 'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/158.0.0.0 Safari/537.36 Edg/158.0.0.0' } #get参数名称 params = { 'q': name } response = requests.get(url, headers=headers, params=params) #获取最终的url print(response.url) #获取请求状态码 print(response.status_code) #获取编码格式 print(response.encoding)
#运行结果:
#打开终端
python .\test3.py
(venv) PS E:\pyCharm\code> python .\test3.py
请输入你要查看的明星名称:蔡徐坤
https://cn.bing.com:443/search?q=蔡徐坤\&toHttps=1\&redig=AAEFF500E28643C8B867667E925B0412
200
utf-8
5.2post请求
#百度翻译 #导入requests包 、 json包 import requests import json word = input('请输入你要翻译的单词:') #1定义一个url url = 'https://fanyi.baidu.com/sug' #2编写一个请求头字典 headers = { 'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/158.0.0.0 Safari/537.36 Edg/158.0.0.0' } #表单 --- post的参数名称 data = { 'kw':word } response = requests.post(url,headers=headers,data=data) #把 JSON 格式字符串 → Python 字典 / 列表 print(json.loads(response.text)) print(response.text)
#运行代码
#打开终端
python .\test4.py
(venv) PS E:\pyCharm\code> py.exe .\test4.py
请输入你要翻译的单词:苹果
{'errno': 0, 'data': {'k': '苹果', 'v': '名. apple'}, {'k': '苹果园', 'v': 'apple grove'}, {'k': '苹果头', 'v': 'apple head'}, {'k': '苹果干', 'v': '\[医dried apple'}, {'k': '苹果木', 'v': 'applewood'}], 'logid': 2385624197}
(venv) PS E:\pyCharm\code>
六、get请求和post请求的区别
1.get请求的参数名字是params ; post请求的参数的名字是data
2.post请求的参数不在url地址中体现