
python
John的IT之旅
有梦想谁都了不起!
展开
-
Python网络爬虫爬取搜狗关键词首页
#!/usr/bin/env python# -*— coding:utf-8 -*-import requests# UA:User-Agent(请求载体的身份标识)# UA检测:门户网站的服务器会检测对应身份的载体身份标识,如果检测到请求的载体身份标识为某一款浏览器# 说明该请求是一个正常的请求。但是,如果检测到请求的载体的身份标识不是基于某一浏览器的,则表示该# 请求为不正常请求(爬虫),则服务器端就很可能拒绝该次请求# UA伪装:让爬虫对应的请求载体身份标识伪装成某一款浏览器i.原创 2022-05-25 19:20:55 · 525 阅读 · 0 评论 -
django后端踩坑合集
1.报错:Invalid HTTP_HOST header: ‘111.230.19.43:8000’. You may need to add '111.230解决:在我们创建的项目里修改setting.py文件ALLOWED_HOSTS = [’*’] #在这里请求的host添加了*2.报错:.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is在__init__.py中加import pymysqlpy原创 2020-08-27 10:58:23 · 254 阅读 · 1 评论 -
Python 实现LeetCode刷题之Z字形变换
class Solution: def convert(self, s: str, numRows: int) -> str: if numRows < 2: return s res = ["" for _ in range(numRows)] i, flag = 0, -1 for c...原创 2020-03-19 15:04:05 · 246 阅读 · 0 评论 -
Python 实现LeetCode刷题之无重复字符的最长子串
class Solution(object): def LengthOfLongestSubstring(self,s): d = {} start = 0 ans = 0 for i,c in enumerate(s): if c in d: start = max(...原创 2020-03-18 15:33:58 · 165 阅读 · 0 评论 -
Python 实现LeetCode刷题之两数相加
加了Python实现单链表的代码。class ListNode: def __init__(self, x): self.val = x self.next = Noneclass LinkList: def __init__(self): self.head=None def initList(self, d...原创 2020-03-18 15:32:41 · 192 阅读 · 0 评论 -
Python 实现LeetCode刷题之两数之和
class Solution(object): def twoSum(self, nums, target): d = {} for i, num in enumerate(nums): if target-num in d: return [d[target-num], i] ...原创 2020-03-18 15:30:37 · 195 阅读 · 0 评论 -
python-opencv 形态学操作
import cv2import numpy as npimg = cv2.imread('images/1.png', 0)kenel = np.ones((5, 5), np.uint8)erosion = cv2.erode(img, kenel, iterations=1) # 腐蚀res = np.hstack((img, erosion))kenel1 = np....原创 2020-03-11 14:17:40 · 209 阅读 · 0 评论 -
python-OpenCV 调用摄像头
import cv2import numpy as npvc = cv2.VideoCapture(0)if vc.isOpened(): opened, frame = vc.read()else: opened = Falsewhile opened: ret, frame = vc.read() if frame is None: ...原创 2020-03-10 17:48:42 · 184 阅读 · 0 评论 -
用python 实现四种经典排序
1选择排序:def SelectSort(relist): len_ = len(relist) for i in range(0, len_): min_index = i for j in range(i + 1, len_): if relist[j] < relist[min_index]: ...原创 2020-03-10 15:38:37 · 218 阅读 · 0 评论 -
python求两点之间的距离使用getattr方法
import mathclass Point: def __init__(self, x, y): self.x = x self.y = y def __repr__(self): return 'Point({!r:},{!r:})'.format(self.x, self.y) def distance(se...原创 2020-03-02 17:15:41 · 308 阅读 · 0 评论 -
python 子类扩展父类的属性
class Person: def __init__(self, name): self.name = name @property def name(self): return self._name @name.setter def name(self, value): if not isinstanc...原创 2020-02-26 16:12:42 · 703 阅读 · 0 评论 -
python子类调动父类方法
class Base: def __init__(self): print('Base.__init__')class Apple(Base): def __init__(self): super().__init__() print('A.__init__')class Banana(Base): def __...原创 2020-02-26 15:31:42 · 147 阅读 · 0 评论 -
python利用property 实现属性的管理
调用属性不用要加()import mathclass Circle: def __init__(self, radius): self.radius = radius @property def area(self): return math.pi*self.radius**2 @property def pe...原创 2020-02-26 14:46:55 · 112 阅读 · 0 评论 -
python简单的用回到函数处理相加运算
class ResultHandler: def __init__(self): self.sequence = 0 def handler(self, result): self.sequence += 1 # print('[{}] Got: {}'.format(self.sequence, result)) ...原创 2020-02-25 16:39:36 · 208 阅读 · 0 评论 -
python小学分数计算
让我们回到小学刚刚学习分数的时候 :from fractions import Fractiona = Fraction(5,4)b = Fraction(7,16)print(a + b)27/16print(a *b)35/64c = a*bc.numerator35c.denominator64float(c)0.546875...原创 2020-01-15 16:24:15 · 432 阅读 · 0 评论 -
关于在python上浮点数无法精确表达出所有十进制小数位
浮点数无法表达出所有的十进制小数位:a = 2.1b = 4.2c = a + bc6.300000000000001(a + b) == 6.3False解决方法可以使用decimal模块:from decimal import Decimala = Decimal('4.2')b = Decimal('2.1')a + bDecimal('6.3')pr...原创 2020-01-14 14:16:11 · 459 阅读 · 0 评论 -
python 设计模式之简单工厂模式
使用简单的工厂模式定义了一个接口创建对象,但是工厂本身并不负责创建对象,而是将这个任务交给子类来完成,即子类决定了要实例化哪些类。from abc import ABCMeta, abstractmethodclass Animal(metaclass=ABCMeta): @abstractmethod def do_say(self): pass...原创 2020-01-09 10:51:22 · 97 阅读 · 0 评论 -
pycharm的一些快捷键
分享一些使用高效的pycharm快捷键1.Ctrl + D可以复制一行代码(windows),MacOS 将Ctrl换成command即可2.Ctrl + shift +上下键可以在选中一行代码的情况下将该行代码上下移动位置3.使用Tap键可以补全代码非常好用...原创 2020-01-09 10:25:39 · 147 阅读 · 0 评论 -
python中想要在两个字典中查找想要的元素
在两个字典中查找元素,并优先选择第一个字典中的元素a = {'x':1, 'z':3}b = {'y':2, 'z':4}from collections import ChainMapc = ChainMap(a,b)print(c['x'])1print(c['y'])2print(c['z'])3...原创 2020-01-07 16:14:06 · 1030 阅读 · 0 评论 -
windows pycharm 更换国内pip源
在C:\Users\用户名\ 下创建 pip 文件夹 再在文件夹内创建pip.ini 文件[global] timeout = 6000 index-url = https://pypi.tuna.tsinghua.edu.cn/simple trusted-host = https://pypi.tuna.tsinghua.edu.cn URL可以自己更换成其...原创 2020-01-07 11:05:55 · 650 阅读 · 0 评论 -
python OpenCV实现调整画面的HSV
OpenCV 调整得到想要的颜色import cv2import numpy as npcap = cv2.VideoCapture(0)#打开摄像头pic=np.zeros((480, 640, 3),dtype=np.uint8)def nothing(): passcv2.namedWindow("HSV",0)cv2.resizeWindow("HSV",50...原创 2020-01-07 10:46:40 · 2122 阅读 · 0 评论 -
基于Python OpenCV的单目标轮廓匹配
python opencv 实现单目标餐盘轮廓识别import cv2import numpy as npdef template(template): temp = cv2.imread(template, 0) # temp_gray = cv2.cvtColor(temp, cv2.COLOR_BGR2GRAY) _, temp_thresh = cv...原创 2020-01-03 10:12:09 · 1872 阅读 · 1 评论