常用方法

Python3 的 math 模块提供了许多数学函数,用于执行常见的数学运算。以下是 math 模块中一些常用方法的简介:

  1. 数值运算函数
  • math.sqrt(x):返回 x 的平方根。
  • math.pow(x, y):返回 x 的 y 次幂。
  • math.exp(x):返回 e 的 x 次幂。
  • math.log(x[, base]):返回 x 的自然对数(以 e 为底),或者可选地返回以指定 base 为底的对数。
  1. 三角函数
  • math.sin(x):返回 x 弧度的正弦值。
  • math.cos(x):返回 x 弧度的余弦值。
  • math.tan(x):返回 x 弧度的正切值。
  • math.radians(x):将角度转换为弧度。
  • math.degrees(x):将弧度转换为角度。
  1. 取整函数
  • math.ceil(x):返回大于等于 x 的最小整数。
  • math.floor(x):返回小于等于 x 的最大整数。
  • math.trunc(x):返回 x 的整数部分。
  1. 常量
  • math.pi:表示圆周率 π。
  • math.e:表示自然对数的底 e。
  1. 其他数学函数
  • math.factorial(x):返回 x 的阶乘。
  • math.modf(x):返回 x 的小数部分和整数部分。
  • math.gcd(a, b):返回 a 和 b 的最大公约数。
  • math.isclose(a, b):检查 a 和 b 是否“足够近”,通常用于浮点数比较。

requests 模块

Python requests 是一个常用的 HTTP 请求库,可以方便地向网站发送 HTTP 请求,并获取响应结果。

requests 模块比 urllib 模块更简洁。

requests 模块是 Python 中用于发送 HTTP 请求的流行库,它提供了简单而优雅的方式来处理 HTTP 请求和响应。以下是 requests 模块的使用详情和举例:

  1. 安装
    如果你的环境中没有安装 requests 模块,你可以使用以下命令通过 pip 进行安装:
pip install requests
  1. 发送 GET 请求
    你可以使用 requests.get 方法来发送一个 GET 请求,并获得响应数据。
import requests
response = requests.get('https://api.example.com/data')
print(response.status_code)  # 输出响应状态码
print(response.text)  # 输出响应文本内容
  1. 发送 POST 请求
    你可以使用 requests.post 方法来发送一个 POST 请求,并携带请求数据。
import requests
payload = {'key1': 'value1', 'key2': 'value2'}
response = requests.post('https://api.example.com/post', data=payload)
print(response.status_code)  # 输出响应状态码
  1. 处理响应
    一旦获得响应对象,你可以访问其各个属性,例如状态码、头部信息、响应内容,并进行错误处理或数据解析。
import requests
response = requests.get('https://api.example.com/data')
if response.status_code == 200:
    print(response.headers)
    print(response.json())
else:
    print("Request failed with status code: " + str(response.status_code))
  1. 处理异常
    在使用 requests 发送请求时,也需要考虑到可能出现的异常情况,比如网络连接问题等。
import requests
try:
    response = requests.get('https://api.example.com/data')
    response.raise_for_status()  # 如果请求不成功,抛出异常
    print(response.text)
except requests.exceptions.RequestException as e:
    print("Request failed: " + str(e))

当使用 requests 模块发送 HTTP 请求时,有时需要处理重定向、设置请求头、以及使用代理。

  1. 处理重定向
    对于重定向,可以使用 allow_redirects 参数控制是否自动处理重定向。默认情况下,allow_redirects 为 True,会自动处理重定向;如果设置为 False,则禁止自动重定向。
import requests
response = requests.get('https://example.com', allow_redirects=False)
if response.status_code == 301 or response.status_code == 302:
    new_location = response.headers['Location']
    response = requests.get(new_location)
    print(response.text)
  1. 设置请求头
    可以使用 headers 参数设置请求头信息,通常用于模拟浏览器发送请求或传递特定的头部信息。
import requests
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get('https://example.com', headers=headers)
print(response.text)
  1. 使用代理
    如果需要通过代理服务器发送请求,可以通过 proxies 参数指定代理服务器的地址。代理可以是 HTTP 代理、HTTPS 代理或 SOCKS 代理。
import requests
proxies = {'http': 'http://<proxy_ip>:<proxy_port>', 'https': 'https://<proxy_ip>:<proxy_port>'}
response = requests.get('https://example.com', proxies=proxies)
print(response.text)

使用中,可以根据具体的需求对请求进行定制化设置,以满足不同的场景要求。

请求返回

每次调用 requests 请求之后,会返回一个 response 对象,该对象包含了具体的响应信息,如状态码、响应头、响应内容等:

# 导入 requests 包
import requests

# 发送请求
x = requests.get('https://www.runoob.com/')

# 返回网页内容
print(x.text)

14 Python进阶:math模块和requests 模块_python


14 Python进阶:math模块和requests 模块_HTTP_02


14 Python进阶:math模块和requests 模块_重定向_03

# 导入 requests 包
import requests

 
kw = {'s':'python 教程'}

# 设置请求头
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}
 
# params 接收一个字典或者字符串的查询参数,字典类型自动转换为url编码,不需要urlencode()
response = requests.get("https://www.runoob.com/", params = kw, headers = headers)

# 查看响应状态码
print (response.status_code)

# 查看响应头部字符编码
print (response.encoding)

# 查看完整url地址
print (response.url)

# 查看响应内容,response.text 返回的是Unicode格式的数据
print(response.text)

关注我,不迷路,共学习,同进步

关注我,不迷路,共学习,同进步

14 Python进阶:math模块和requests 模块_状态码_04