Python——requests模块详解

news2024/10/6 16:23:29

1、模块说明

requests是使用Apache2 licensed 许可证的HTTP库。

用python编写。

比urllib2模块更简洁。

Request支持HTTP连接保持和连接池,支持使用cookie保持会话,支持文件上传,支持自动响应内容的编码,支持国际化的URL和POST数据自动编码。

在python内置模块的基础上进行了高度的封装,从而使得python进行网络请求时,变得人性化,使用Requests可以轻而易举的完成浏览器可有的任何操作。

现代,国际化,友好。

requests会自动实现持久连接keep-alive

2、基础入门

1)导入模块

import requests

2)发送请求的简洁

  示例代码:获取一个网页(个人github)

import requests

r = requests.get('https://github.com/Ranxf')       # 最基本的不带参数的get请求
r1 = requests.get(url='http://dict.baidu.com/s', params={'wd': 'python'})      # 带参数的get请求

我们就可以使用该方式使用以下各种方法

1   requests.get(‘https://github.com/timeline.json’)                                # GET请求
2   requests.post(“http://httpbin.org/post”)                                        # POST请求
3   requests.put(“http://httpbin.org/put”)                                          # PUT请求
4   requests.delete(“http://httpbin.org/delete”)                                    # DELETE请求
5   requests.head(“http://httpbin.org/get”)                                         # HEAD请求
6   requests.options(“http://httpbin.org/get” )                                     # OPTIONS请求

3)为url传递参数

>>> url_params = {'key':'value'}       #    字典传递参数,如果值为None的键不会被添加到url中
>>> r = requests.get('your url',params = url_params)
>>> print(r.url)
  your url?key=value

4)响应的内容

r.encoding                       #获取当前的编码
r.encoding = 'utf-8'             #设置编码
r.text                           #以encoding解析返回内容。字符串方式的响应体,会自动根据响应头部的字符编码进行解码。
r.content                        #以字节形式(二进制)返回。字节方式的响应体,会自动为你解码 gzip 和 deflate 压缩。

r.headers                        #以字典对象存储服务器响应头,但是这个字典比较特殊,字典键不区分大小写,若键不存在则返回None

r.status_code                     #响应状态码
r.raw                             #返回原始响应体,也就是 urllib 的 response 对象,使用 r.raw.read()   
r.ok                              # 查看r.ok的布尔值便可以知道是否登陆成功
 #*特殊方法*#
r.json()                         #Requests中内置的JSON解码器,以json形式返回,前提返回的内容确保是json格式的,不然解析出错会抛异常
r.raise_for_status()             #失败请求(非200响应)抛出异常

post发送json请求:

1 import requests
2 import json
3  
4 r = requests.post('https://api.github.com/some/endpoint', data=json.dumps({'some': 'data'}))
5 print(r.json())

5)定制头和cookie信息

header = {'user-agent': 'my-app/0.0.1''}
cookie = {'key':'value'}
 r = requests.get/post('your url',headers=header,cookies=cookie) 
data = {'some': 'data'}
headers = {'content-type': 'application/json',
           'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0'}
 
r = requests.post('https://api.github.com/some/endpoint', data=data, headers=headers)
print(r.text)

6)响应状态码

使用requests方法后,会返回一个response对象,其存储了服务器响应的内容,如上实例中已经提到的 r.text、r.status_code……
获取文本方式的响应体实例:当你访问 r.text 之时,会使用其响应的文本编码进行解码,并且你可以修改其编码让 r.text 使用自定义的编码进行解码。

1 r = requests.get('http://www.itwhy.org')
2 print(r.text, '\n{}\n'.format('*'*79), r.encoding)
3 r.encoding = 'GBK'
4 print(r.text, '\n{}\n'.format('*'*79), r.encoding)

示例代码:

1 import requests
2 
3 r = requests.get('https://github.com/Ranxf')       # 最基本的不带参数的get请求
4 print(r.status_code)                               # 获取返回状态
5 r1 = requests.get(url='http://dict.baidu.com/s', params={'wd': 'python'})      # 带参数的get请求
6 print(r1.url)
7 print(r1.text)        # 打印解码后的返回数据

运行结果:

/usr/bin/python3.5 /home/rxf/python3_1000/1000/python3_server/python3_requests/demo1.py
200
http://dict.baidu.com/s?wd=python
…………

Process finished with exit code 0
 r.status_code                      #如果不是200,可以使用 r.raise_for_status() 抛出异常

7)响应

r.headers                                  #返回字典类型,头信息
r.requests.headers                         #返回发送到服务器的头信息
r.cookies                                  #返回cookie
r.history                                  #返回重定向信息,当然可以在请求是加上allow_redirects = false 阻止重定向

8)超时

r = requests.get('url',timeout=1)           #设置秒数超时,仅对于连接有效

9)会话对象,能够跨请求保持某些参数

s = requests.Session()
s.auth = ('auth','passwd')
s.headers = {'key':'value'}
r = s.get('url')
r1 = s.get('url1') 

10)代理

proxies = {'http':'ip1','https':'ip2' }
requests.get('url',proxies=proxies)

汇总:

# HTTP请求类型
# get类型
r = requests.get('https://github.com/timeline.json')
# post类型
r = requests.post("http://m.ctrip.com/post")
# put类型
r = requests.put("http://m.ctrip.com/put")
# delete类型
r = requests.delete("http://m.ctrip.com/delete")
# head类型
r = requests.head("http://m.ctrip.com/head")
# options类型
r = requests.options("http://m.ctrip.com/get")

# 获取响应内容
print(r.content) #以字节的方式去显示,中文显示为字符
print(r.text) #以文本的方式去显示

#URL传递参数
payload = {'keyword': '香港', 'salecityid': '2'}
r = requests.get("http://m.ctrip.com/webapp/tourvisa/visa_list", params=payload) 
print(r.url) #示例为http://m.ctrip.com/webapp/tourvisa/visa_list?salecityid=2&keyword=香港

#获取/修改网页编码
r = requests.get('https://github.com/timeline.json')
print (r.encoding)


#json处理
r = requests.get('https://github.com/timeline.json')
print(r.json()) # 需要先import json    

# 定制请求头
url = 'http://m.ctrip.com'
headers = {'User-Agent' : 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 4 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19'}
r = requests.post(url, headers=headers)
print (r.request.headers)

#复杂post请求
url = 'http://m.ctrip.com'
payload = {'some': 'data'}
r = requests.post(url, data=json.dumps(payload)) #如果传递的payload是string而不是dict,需要先调用dumps方法格式化一下

# post多部分编码文件
url = 'http://m.ctrip.com'
files = {'file': open('report.xls', 'rb')}
r = requests.post(url, files=files)

# 响应状态码
r = requests.get('http://m.ctrip.com')
print(r.status_code)
    
# 响应头
r = requests.get('http://m.ctrip.com')
print (r.headers)
print (r.headers['Content-Type'])
print (r.headers.get('content-type')) #访问响应头部分内容的两种方式
    
# Cookies
url = 'http://example.com/some/cookie/setting/url'
r = requests.get(url)
r.cookies['example_cookie_name']    #读取cookies
    
url = 'http://m.ctrip.com/cookies'
cookies = dict(cookies_are='working')
r = requests.get(url, cookies=cookies) #发送cookies

#设置超时时间
r = requests.get('http://m.ctrip.com', timeout=0.001)

#设置访问代理
proxies = {
           "http": "http://10.10.1.10:3128",
           "https": "http://10.10.1.100:4444",
          }
r = requests.get('http://m.ctrip.com', proxies=proxies)


#如果代理需要用户名和密码,则需要这样:
proxies = {
    "http": "http://user:pass@10.10.1.10:3128/",
}
# HTTP请求类型
# get类型
r = requests.get('https://github.com/timeline.json')
# post类型
r = requests.post("http://m.ctrip.com/post")
# put类型
r = requests.put("http://m.ctrip.com/put")
# delete类型
r = requests.delete("http://m.ctrip.com/delete")
# head类型
r = requests.head("http://m.ctrip.com/head")
# options类型
r = requests.options("http://m.ctrip.com/get")

# 获取响应内容
print(r.content) #以字节的方式去显示,中文显示为字符
print(r.text) #以文本的方式去显示

#URL传递参数
payload = {'keyword': '香港', 'salecityid': '2'}
r = requests.get("http://m.ctrip.com/webapp/tourvisa/visa_list", params=payload) 
print(r.url) #示例为http://m.ctrip.com/webapp/tourvisa/visa_list?salecityid=2&keyword=香港

#获取/修改网页编码
r = requests.get('https://github.com/timeline.json')
print (r.encoding)


#json处理
r = requests.get('https://github.com/timeline.json')
print(r.json()) # 需要先import json    

# 定制请求头
url = 'http://m.ctrip.com'
headers = {'User-Agent' : 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 4 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19'}
r = requests.post(url, headers=headers)
print (r.request.headers)

#复杂post请求
url = 'http://m.ctrip.com'
payload = {'some': 'data'}
r = requests.post(url, data=json.dumps(payload)) #如果传递的payload是string而不是dict,需要先调用dumps方法格式化一下

# post多部分编码文件
url = 'http://m.ctrip.com'
files = {'file': open('report.xls', 'rb')}
r = requests.post(url, files=files)

# 响应状态码
r = requests.get('http://m.ctrip.com')
print(r.status_code)
    
# 响应头
r = requests.get('http://m.ctrip.com')
print (r.headers)
print (r.headers['Content-Type'])
print (r.headers.get('content-type')) #访问响应头部分内容的两种方式
    
# Cookies
url = 'http://example.com/some/cookie/setting/url'
r = requests.get(url)
r.cookies['example_cookie_name']    #读取cookies
    
url = 'http://m.ctrip.com/cookies'
cookies = dict(cookies_are='working')
r = requests.get(url, cookies=cookies) #发送cookies

#设置超时时间
r = requests.get('http://m.ctrip.com', timeout=0.001)

#设置访问代理
proxies = {
           "http": "http://10.10.1.10:3128",
           "https": "http://10.10.1.100:4444",
          }
r = requests.get('http://m.ctrip.com', proxies=proxies)


#如果代理需要用户名和密码,则需要这样:
proxies = {
    "http": "http://user:pass@10.10.1.10:3128/",
}

3、示例代码

GET请求

1 # 1、无参数实例
 2   
 3 import requests
 4   
 5 ret = requests.get('https://github.com/timeline.json')
 6   
 7 print(ret.url)
 8 print(ret.text)
 9   
10   
11   
12 # 2、有参数实例
13   
14 import requests
15   
16 payload = {'key1': 'value1', 'key2': 'value2'}
17 ret = requests.get("http://httpbin.org/get", params=payload)
18   
19 print(ret.url)
20 print(ret.text)

POST请求

# 1、基本POST实例
  
import requests
  
payload = {'key1': 'value1', 'key2': 'value2'}
ret = requests.post("http://httpbin.org/post", data=payload)
  
print(ret.text)
  
  
# 2、发送请求头和数据实例
  
import requests
import json
  
url = 'https://api.github.com/some/endpoint'
payload = {'some': 'data'}
headers = {'content-type': 'application/json'}
  
ret = requests.post(url, data=json.dumps(payload), headers=headers)
  
print(ret.text)
print(ret.cookies)

json请求:

#! /usr/bin/python3
import requests
import json


class url_request():
    def __init__(self):
        ''' init '''

if __name__ == '__main__':
    heard = {'Content-Type': 'application/json'}
    payload = {'CountryName': '中国',
               'ProvinceName': '四川省',
               'L1CityName': 'chengdu',
               'L2CityName': 'yibing',
               'TownName': '',
               'Longitude': '107.33393',
               'Latitude': '33.157131',
               'Language': 'CN'}
    r = requests.post("http://www.xxxxxx.com/CityLocation/json/LBSLocateCity", heards=heard, data=payload)
    data = r.json()
    if r.status_code!=200:
        print('LBSLocateCity API Error' + str(r.status_code))
    print(data['CityEntities'][0]['CityID'])  # 打印返回json中的某个key的value
    print(data['ResponseStatus']['Ack'])
    print(json.dump(data, indent=4, sort_keys=True, ensure_ascii=False))  # 树形打印json,ensure_ascii必须设为False否则中文会显示为unicode

Xml请求:

#! /usr/bin/python3
import requests

class url_request():
    def __init__(self):
        """init"""

if __name__ == '__main__':
    heards = {'Content-type': 'text/xml'}
    XML = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><Request xmlns="http://tempuri.org/"><jme><JobClassFullName>WeChatJSTicket.JobWS.Job.JobRefreshTicket,WeChatJSTicket.JobWS</JobClassFullName><Action>RUN</Action><Param>1</Param><HostIP>127.0.0.1</HostIP><JobInfo>1</JobInfo><NeedParallel>false</NeedParallel></jme></Request></soap:Body></soap:Envelope>'
    url = 'http://jobws.push.mobile.xxxxxxxx.com/RefreshWeiXInTokenJob/RefreshService.asmx'
    r = requests.post(url=url, heards=heards, data=XML)
    data = r.text
    print(data)

状态异常处理

import requests

URL = 'http://ip.taobao.com/service/getIpInfo.php'  # 淘宝IP地址库API
try:
    r = requests.get(URL, params={'ip': '8.8.8.8'}, timeout=1)
    r.raise_for_status()  # 如果响应状态码不是 200,就主动抛出异常
except requests.RequestException as e:
    print(e)
else:
    result = r.json()
    print(type(result), result, sep='\n')

上传文件

使用request模块,也可以上传文件,文件的类型会自动进行处理:

import requests
 
url = 'http://127.0.0.1:8080/upload'
files = {'file': open('/home/rxf/test.jpg', 'rb')}
#files = {'file': ('report.jpg', open('/home/lyb/sjzl.mpg', 'rb'))}     #显式的设置文件名
 
r = requests.post(url, files=files)
print(r.text)

request更加方便的是,可以把字符串当作文件进行上传:

import requests
 
url = 'http://127.0.0.1:8080/upload'
files = {'file': ('test.txt', b'Hello Requests.')}     #必需显式的设置文件名
 
r = requests.post(url, files=files)
print(r.text)

6) 身份验证

基本身份认证(HTTP Basic Auth)

import requests
from requests.auth import HTTPBasicAuth
 
r = requests.get('https://httpbin.org/hidden-basic-auth/user/passwd', auth=HTTPBasicAuth('user', 'passwd'))
# r = requests.get('https://httpbin.org/hidden-basic-auth/user/passwd', auth=('user', 'passwd'))    # 简写
print(r.json())

另一种非常流行的HTTP身份认证形式是摘要式身份认证,Requests对它的支持也是开箱即可用的:

requests.get(URL, auth=HTTPDigestAuth('user', 'pass')

Cookies与会话对象

如果某个响应中包含一些Cookie,你可以快速访问它们:

import requests
 
r = requests.get('http://www.google.com.hk/')
print(r.cookies['NID'])
print(tuple(r.cookies))

要想发送你的cookies到服务器,可以使用 cookies 参数:

import requests
 
url = 'http://httpbin.org/cookies'
cookies = {'testCookies_1': 'Hello_Python3', 'testCookies_2': 'Hello_Requests'}
# 在Cookie Version 0中规定空格、方括号、圆括号、等于号、逗号、双引号、斜杠、问号、@,冒号,分号等特殊符号都不能作为Cookie的内容。
r = requests.get(url, cookies=cookies)
print(r.json())

会话对象让你能够跨请求保持某些参数,最方便的是在同一个Session实例发出的所有请求之间保持cookies,且这些都是自动处理的,甚是方便。
下面就来一个真正的实例,如下是快盘签到脚本:

requests模块抓取网页源码并保存到文件示例

这是一个基本的文件保存操作,但这里有几个值得注意的问题:

1.安装requests包,命令行输入pip install requests即可自动安装。很多人推荐使用requests,自带的urllib.request也可以抓取网页源码

2.open方法encoding参数设为utf-8,否则保存的文件会出现乱码。

3.如果直接在cmd中输出抓取的内容,会提示各种编码错误,所以保存到文件查看。

4.with open方法是更好的写法,可以自动操作完毕后释放资源

#! /urs/bin/python3
import requests

'''requests模块抓取网页源码并保存到文件示例'''
html = requests.get("http://www.baidu.com")
with open('test.txt', 'w', encoding='utf-8') as f:
    f.write(html.text)
    
'''读取一个txt文件,每次读取一行,并保存到另一个txt文件中的示例'''
ff = open('testt.txt', 'w', encoding='utf-8')
with open('test.txt', encoding="utf-8") as f:
    for line in f:
        ff.write(line)
        ff.close()

因为在命令行中打印每次读取一行的数据,中文会出现编码错误,所以每次读取一行并保存到另一个文件,这样来测试读取是否正常。(注意open的时候制定encoding编码方式)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/66484.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

公众号查题系统

公众号查题系统 本平台优点&#xff1a; 多题库查题、独立后台、响应速度快、全网平台可查、功能最全&#xff01; 1.想要给自己的公众号获得查题接口&#xff0c;只需要两步&#xff01; 2.题库&#xff1a; 查题校园题库&#xff1a;查题校园题库后台&#xff08;点击跳转…

Unity计算着色器 02

序 在百度了一下显卡以后&#xff0c;总结的话就是这个图。 这个图再总结的话就是左下角的那个框子&#xff0c;一个任务可以分为若干个线程组&#xff0c;每个线程组包含若干个线程。 两个若干&#xff0c;可以看成是两个变量&#xff0c;而且是有关系的——它俩乘积是一个…

如何使用PowerShell脚本进行数字签名?

如何对PowerShell脚本进行数字签名以进行身份验证和保护的快速指南。 您是否希望确保您的脚本可以安全使用并且没有被篡改&#xff0c;从而伤害用户&#xff1f;如果是这种情况&#xff0c;那么您将需要使用由已知证书颁发机构&#xff08;如Sectigo、DigiCert&#xff09;提供…

简谈Spring Boot3.0升级后的踩坑之旅,另附解决方案!

注&#xff1a;本文基于 newbeemall 项目升级Spring Boot3.0踩坑总结而来。 一. 编译报错&#xff0c;import javax.servlet.*; 不存在 这个报错主要是Spring Boot3.0已经为所有依赖项从 Java EE 迁移到 Jakarta EE API&#xff0c;导致 servlet 包名的修改&#xff0c;Sprin…

写了30000字 用 shell 脚本做自动化测试实战项目(全面、完整)

前言 项目中有一个功能&#xff0c;需要监控本地文件系统的变更&#xff0c;例如文件的增、删、改名、文件数据变动等等。之前只在 windows 上有实现&#xff0c;采用的是 iocp ReadDirectoryChanges 方案&#xff0c;现在随着整个应用移植到 mac 上&#xff0c;需要对这一部…

大数据智能交通未来会是怎样的交通状况?

大数据智能交通未来会是怎样的交通状况&#xff1f;智能交通也是智慧城市的一个重要组成部分将会改变我们的交通&#xff0c;交通少不了大数据的处理&#xff0c;人工智能自动驾驶离不开大数据的支撑。 实现智能交通需要高效地从海量数据中分析、挖掘所需的信息和规律&#xf…

缺少微信小程序测试经验?这篇文章带你从0开始

微信小程序已经越来越普遍&#xff0c;但目前接触小程序的项目相对较少&#xff0c;对小程序的特性也不了解&#xff0c;缺少小程序测试实战经验。 本文主要通过对微信小程序特性和测试点进行总结&#xff0c;储备测试知识&#xff0c;提高测试效率。 小程序发布审核 发布前…

Linux jprobe的使用和原理

文章目录前言一、demo1.1 demo演示1.2 struct jprobe二、jprobe 原理2.1 原理简介1.2 原理详解三、源码解析3.1 struct jprobe3.2 register_jprobe3.3 setjmp_pre_handler3.4 jprobe_return3.5 longjmp_break_handler四、Deprecated Features五、使用 perf-probe 获取函数参数总…

CentOS7一键安装OpenStack

环境 CentOS 7 CPU核心数&#xff1a;2x2 RAM&#xff1a;8G DISK&#xff1a;60G 问题说明 在安装openstack过程中&#xff0c;一直卡在下面过程&#xff1a; Testing if puppet apply is finished: 192.168.100.132_controller.pp [ | ]等待一会儿之后会报各种不同的错误…

crontab 实现秒级定时任务的执行(学习笔记)

crontab 实现秒级定时任务的执行 传统暴力法 crontab -e ***** /usr/bin/curl 地址 ***** sleep 10; /usr/bin/curl 地址 ***** sleep 20; /usr/bin/curl 地址地址 ***** sleep 30; /usr/bin/curl 地址 ***** sleep 40; /usr/bin/curl 地址 ***** sleep 50; /usr/bin/cu…

进程和计划任务管理

查看进程信息 ps ps命令 查看静态的进程统计信息 ps -elf 查看进程信息 top top命令 查看动态的进程排名信息 top 查看进程信息 pgrep pgrep命令 根据特定条件查询进程 PID 信息 pgrep -l “log” pgrep -l -U teacher -t tty1 查看进程信息 pstree pstree命令 以树…

面试害怕考到JVM? 看这一篇就够了~

目录 前言 一、JVM内存划分 二、类加载 2.1、类加载是在干什么&#xff1f; 2.2、类加载的过程 2.3、何时触发类加载&#xff1f; 2.4、双亲委派模型&#xff08;重点考察&#xff09; 2.4.1、什么是双亲委派模型&#xff1f; 2.4.2、涉及到的类加载器 2.4.3、详细过…

【树莓派】raspberry pi控制超声波测距

目录一、超声波1、模块介绍2、工作原理二、gettimeofday函数三、树莓派控制超声波测距一、超声波 1、模块介绍 简介&#xff1a;   超声波传感器模块上面通常有两个超声波元器件&#xff0c;一个用于发射&#xff0c;一个用于接收。 硬件: 电路板上有4个引脚&#xff1a; …

测试面试被问“期望薪资多少”,不要傻傻直接报价,高情商都这样说

对于软件测试从业者而言&#xff0c;面试很重要&#xff0c;因为那是拿到薪资报酬丰厚程度的关键&#xff0c;你的理论及实操经验确实都很棒&#xff0c;那就尽量别让自己的面试表现拖自己的后腿&#xff0c;否则大概率会让你的薪水大打折扣。 你在面试中是否也遇到很多次以下…

38寻找二叉树的最近公共祖先39序列化和反序列化

38.寻找二叉树的最近公共祖先 这题和上一题的区别在于这不是二叉搜索树&#xff0c;无法根据值的大小来判断节点的位置&#xff0c;于是需要遍历 法1 递归写法 递归在左右子树寻找o1和o2 import java.util.*;/** public class TreeNode {* int val 0;* TreeNode left …

12月编程语言排行榜公布啦~

2022年迎来了最后一个月&#xff0c;我们可以看到&#xff0c;在这一年中编程语言起起伏伏&#xff0c;有的语言始终炙手可热&#xff0c;而有的语言却逐渐“没落”...... 日前&#xff0c;全球知名TIOBE编程语言社区发布了12月编程语言排行榜&#xff0c;有哪些新变化&#x…

Test Squence测试过程中如何按照特定条件暂停或者停止仿真

在Simulink模型做Test Squence测试时&#xff0c;工程师有时候希望测试用例能按照自己期望的条件来停止或暂停仿真&#xff0c;这个期望的特定条件&#xff0c;可以是时间达到&#xff0c;也可以是任何能达到的特定状态。 具体实现方法如下&#xff1a; 1、在Test Harness测试…

公司 CTO:高性能开发,你不会 Netty,怎么好意思拿 20K?

主管&#xff1a;这个版块用 Netty 框架就可以了呀&#xff0c;不会吗&#xff1f; &#xff08;此时&#xff0c;公司 CTO 路过&#xff09; 某程序员&#xff1a;这个我真不会... 主管&#xff1a;好了好了&#xff0c;那这一块我交给别人去做&#xff0c;这个也不难啊&am…

代码随想录刷题记录day36 整数拆分+不同的二叉搜索树

代码随想录刷题记录day36 整数拆分不同的二叉搜索树 参考&#xff1a;代码随想录 343. 整数拆分 思想 一个数可以被拆分成2个数或者3个及以上的数。 dp[i]表示拆分i以后&#xff0c;得到的最大的乘积 拆分成两个数 j和i-j,拆分成三个数及以上 j 和dp[i-j]&#xff0c;dp[i…

面试10分钟就完事了,问的实在是太...

干了两年外包&#xff0c;本来想出来正儿八经找个互联网公司上班&#xff0c;没想到算法死在另一家厂子。 自从加入这家外包公司&#xff0c;每天都在加班&#xff0c;钱倒是给的不少&#xff0c;所以也就忍了。没想到11月一纸通知&#xff0c;所有人不许加班&#xff0c;薪资…