Skip to content

Python

约 673 字大约 2 分钟

Python

2024-03-16

Python

global 定义的变量,表明其作用域在局部以外,即局部函数执行完之后,不销毁

python -m pip install PyMySQL fetchone() : 返回单个的元组,也就是一条记录(row),如果没有结果 则返回 None fetchall() : 返回多个元组,即返回多个记录(rows),如果没有结果 则返回 ()

lists = []
lists.append({"no":10,"name":'a'})
lists.append({"no":11,"name":'b'})
lists.append({"no":21,"name":'c'})
print(len(lists)) //length

PIP

pip配置文件地址%APPDATA%\pip\pip.ini

pip install version [packname] # 查看包的可用版本
pip cache purge # 清楚缓存
pip config list # 配置列表

字典

dit={} dit['dddd']="";//通过键修改对应的值,如果没有该键,则会自动添加一个键值对 del 删除,如果没有的话就会报错 del info['stu01']

标准删除姿势

info.pop('stu01')
info.popitem()

查找,首先要确定字典里面有键,有就返回值,没有的话返回 None,不会报错,推荐

print(info.get('stu05'))

判断字典中有没有一个数据,在的话返回True,否则返回False

print('stu04' in info)

info.has_key('stu03')

lastsp=int(lasttime.timestamp()) nowsp = int(time.time())

url转码 urllib.parse.

urllib.parse.quote_plus

str.encode(s)
bytes.decode(b) # 一般返回的content是byte的就decode bytes(s, encoding = "utf8")
str(b, encoding = "utf-8")

字符串拆分

a='12345' a[0:2]#12

index():寻找某字符串位置 没有会报错 建议用find find(): 返回第一个匹配到的索引 rfind(): 返回最后一个匹配到的索引 补位 ljust(len,str)字符向左对齐,用str补齐长度 rjust(len,str)字符向右对齐,用str补齐长度 rjust(len,str)字符中间对齐,用str补齐长度

过滤html标签

re.compile('<[^>]+>').sub("",answer)

403:一般是请求头不对 图片url的时候要加入Picreferer

全局变量在方法中复制 需要global 声明

BeautifulSoup,SoupStrainer 模式1: SoupStrainer:
short_string = SoupStrainer(class_="bd_ddd") soup = BeautifulSoup(data,'html.parser',parse_only=short_string).prettify() 重点:html.parser, prettify():格式化

识别验证码通常是这几个步骤:     1、灰度处理     2、二值化     3、去除边框(如果有的话)     4、降噪     5、切割字符或者倾斜度矫正     6、训练字体库     7、识别

常用模块: 文件 请求

selenium

1.安装 `pip install selenium`
2.下载对应浏览器驱动 http://chromedriver.chromium.org/downloads
3.引用
 ```python
	from selenium import webdriver
	driver = webdriver.Chrome() #括号内可以放下载的驱动的位置 或者把驱动放在python根目录
   ```

django

pip install django #安装
django-admin startproject 项目名称 #创建项目
python manage.py runserver 0.0.0.0:8000 # 启动项目 之后浏览器输出地址查看 https://www.runoob.com/django/django-first-app.html

from fake_useragent import UserAgent
"user-agent": UserAgent(verify_ssl=False).random

问题解决

  • 下载或更新一直超时 源的问题 试试开梯子