面试题
| -可变类型和不可变类型 |
| |
| Set(集合)、List(列表)、Dictionary(字典) |
| 值改变 内存地址不变 |
| |
| |
| Number(数字)、String(字符串)、Tuple(元组) |
| 值改变 内存地址也改变 |
| |
| |
| -常用的魔法方法---> 某种情况下自动触发 |
| |
| |
| 1.new方法才是一个对象实例化的时候所调用的第一个方法,它才是真正意义上的构造方法 |
| 2.它的第一个参数是这个类,其他的参数是用来直接传递给__init__方法。 |
| 3.__new_-决定是否要使用__init__方法,因为new方法可以调用其他类的构造方法或者直接返回别的实例都西昂来作为 本类的实例,如果__new__没有返回实例对象,那么__init__不会被调用 |
| 4.__new__主要是用于继承一个不可变的类型比如一个tuple或者string |
| |
| |
| |
| 类加括号 对象进行初始化 |
| |
| |
| person=Person() -->对象加括号 person()---》触发 |
| |
| |
| 对象.属性 属性不存在 触发 |
| |
| |
| 对象.属性=值,触发 |
| |
| |
| 对象['属性'],属性不存在,触发 |
| |
| |
| 对象['属性']=值,触发 |
| |
| 上下文管理器:只要重写了__enter__和__exit__方法,就具备这个能力 |
| with 对象 as xx: |
| 1 写了一行代码,触发__enter__的执行 |
| 2 写了一行代码,触发__exit__,做一些资源清理工作 |
| |
| |
| |
| -classmethod |
| -staticmethod |
| -如何把方法包装成数据属性 property装饰器 |
| -类中如何隐藏属性 |
| __属性、方法 |
| |
| |
| -双写一致性 |
| -断点续传 |
| -迅雷多线程下载 |
| -内网穿透:https://zhuanlan.zhihu.com/p/370483324 |
| -缓存击穿 |
| |
魔法方法博客复习:https://www.cnblogs.com/liuqingzheng/articles/9949568.html
类的property特性
| property装饰器用于将被装饰的方法伪装成一个数据属性,再使用时可以不加括号而直接使用, |
| |
| property属性的定义和调用要注意以下几点: |
| 1.定义时,在实例方法的基础上添加@property 装饰器:并且仅有一个self参数 |
| 2.调用时,无需括号 |
| |
| |
| class Person: |
| def __init__(self): |
| self.__name='wei' |
| |
| @property |
| def name(self): |
| return self.__name |
| |
| @name.setter |
| def name(self,value): |
| if value.startswith('pig'): |
| print('不能以pig开头,放不进去') |
| else: |
| self.__name=value |
| |
| wei=Person() |
| |
| print(wei.name) |
| |
| wei.name='s_longge' |
| print(wei.name) |
今日内容
1 requests 高级用法
1.0解析json
| |
| |
| import requests |
| data = { |
| 'cname':'', |
| 'pid': '', |
| 'keyword': '500', |
| 'pageIndex': 1, |
| 'pageSize': 10, |
| } |
| res = requests.post('http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword',data=data) |
| |
| print(type(res.json())) |
1.1 ssl认证(了解)
| |
| |
| HTTP+SSL / TLS 也就是在 https上又加了一层处理加密信息得模块,比 http安全,可防止数据在传输过程中被窃取、改变,确保数据的完整性 |
| https://zhuanlan.zhihu.com/p/561907474 |
| |
| |
| |
| 1 不验证证书 |
| import requests |
| respone=requests.get('https://www.12306.cn',verify=False) |
| print(respone.status_code) |
| |
| |
| 2 关闭警告 |
| import requests |
| from requests.packages import urllib3 |
| urllib3.disable_warnings() |
| |
| respone=requests.get('https://www.12306.cn',verify=False) |
| print(respone.status_code) |
| |
| 3 手动携带证书(了解) |
| import requests |
| respone=requests.get('https://www.12306.cn', |
| cert=('/path/server.crt', |
| '/path/key')) |
| print(respone.status_code) |
| |
1.2 使用代理(重要)
| |
| |
| |
| |
| |
| res = requests.post('https://www.cnblogs.com',proxies={'http':'地址+端口'}) |
| |
| |
| res = requests.post('https://www.cnblogs.com',proxies={'http':'60.167.91.34:33080'}) |
| print(res.status_code) |
| |
| |
| |
| -高匿,服务端拿不到真实客户端的ip地址 |
| -透明:服务端能拿到真实客户端的ip地址 |
| |
| -后端如何拿到真实客户端ip地址 |
| -http请求头中有个: X-Forwarded-For: client1,proxy1, proxy2, proxy3 |
| -x-forword-for |
| -获得HTTP请求端真实的IP |
1.3 超时设置
| import requests |
| respone=requests.get('https://www.baidu.com',timeout=0.0001) |
1.4 异常处理
| import requests |
| from requests.exceptions import * |
| requests.exceptions 获取异常类型 |
| |
| try: |
| r=requests.get('http://www.baidu.com',timeout=0.00001) |
| except ReadTimeout: |
| print('===:') |
| |
| |
| |
| |
| |
| except RequestException: |
| print('Error') |
1.5 上传文件
| |
| import requests |
| files = {'file':open('美女.png','rb')} |
| respone = requests.post('http://httpbin.org/post',files=files) |
| print(respone.status_code) |
2 代理池搭建
| |
| |
| -搭建免费的代理池:https://github.com/jhao104/proxy_pool |
| -python:爬虫+flask写的 |
| -架构:看下图 |
| |
| |
| |
| 1 git clone |
| https://github.com/jhao104/proxy_pool.git |
| 2 使用pycharm打开 |
| 3 安装依赖: pip install -r requirements.txt |
| 4 修改配置文件(redis地址即可) |
| HOST = "0.0.0.0" |
| PORT = 5010 |
| DB_CONN = 'redis://127.0.0.1:6379/0' |
| PROXY_FETCHER |
| 5 启动爬虫程序 |
| python proxyPool.py schedule |
| 6 启动服务端 |
| python proxyPool.py server |
| |
| 7 使用随机一个免费代理 |
| 地址栏中输入:http://127.0.0.1:5010/get/ |
| |
| |
| |
| import requests |
| from requests.packages import urllib3 |
| urllib3.disable_warnings() |
| |
| res = requests.get('http://127.0.0.1:5010/get/').json() |
| proxies = {} |
| if res['https']: |
| proxies['https'] = res['proxy'] |
| else: |
| proxies['http'] = res['proxy'] |
| print(proxies) |
| res = requests.post('https://www.cnblogs.com', proxies=proxies,verify=False) |
| |
| print(res) |
2.1 django后端获取客户端的ip
| |
| |
| |
| |
| |
| path('', index), |
| |
| def index(request): |
| ip = request.META.get('REMOTE_ADDR') |
| print('ip地址是', ip) |
| return HttpResponse(ip) |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| from threading import Thread |
| import requests |
| |
| |
| def task(): |
| res = requests.get('http://101.43.19.239/') |
| print(res.text) |
| |
| |
| for i in range(10000000): |
| t = Thread(target=task) |
| t.start() |
| |
3 爬取某视频网站
| import requests |
| import re |
| |
| res = requests.get('https://www.pearvideo.com/category_loading.jsp?reqType=5&categoryId=1&start=0') |
| |
| |
| video_list = re.findall('<a href="(.*?)" class="vervideo-lilink actplay">', res.text) |
| |
| for i in video_list: |
| |
| video_id = i.split('_')[-1] |
| real_url = 'https://www.pearvideo.com/' + i |
| |
| headers = { |
| 'Referer': 'https://www.pearvideo.com/video_%s' % video_id |
| } |
| res1 = requests.get('https://www.pearvideo.com/videoStatus.jsp?contId=%s&mrd=0.29636538326105044' % video_id, |
| headers=headers).json() |
| |
| mp4_url = res1["videoInfo"]['videos']['srcUrl'] |
| mp4_url = mp4_url.replace(mp4_url.split('/')[-1].split('-')[0], 'cont-%s' % video_id) |
| print(mp4_url) |
| res2 = requests.get(mp4_url) |
| with open('./video/%s.mp4' % video_id, 'wb') as f: |
| for line in res2.iter_content(): |
| f.write(line) |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| mp4_url = 'https://video.pearvideo.com/mp4/short/20171204/ 1678938313577-11212458-hd.mp4' |
| |
4 爬取新闻
| import requests |
| |
| from bs4 import BeautifulSoup |
| |
| res = requests.get('https://www.autohome.com.cn/all/1/#liststart') |
| |
| |
| |
| soup = BeautifulSoup(res.text, 'html.parser') |
| |
| ul_list = soup.find_all(name='ul', class_='article') |
| for ul in ul_list: |
| li_list = ul.find_all(name='li') |
| |
| for li in li_list: |
| h3 = li.find(name='h3') |
| if h3: |
| title = h3.text |
| url = 'https:' + li.find('a').attrs['href'] |
| desc = li.find('p').text |
| img = li.find(name='img').attrs['src'] |
| print(''' |
| 新闻标题:%s |
| 新闻连接:%s |
| 新闻摘要:%s |
| 新闻图片:%s |
| ''' % (title, url, desc, img)) |
| |
| |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构