Python
写Bug 的大潘
知其然,而后知其所以然,加油呀!
展开
-
PyPDF2更新到3.0.0版本后需要更改的方法
PDF操作库 PyPdf2原创 2023-08-18 17:34:52 · 4342 阅读 · 0 评论 -
pyppeteer 坑:page.on(‘response was never awaited Enable tracemalloc to get the object allocation trac
异常回顾:python版本:3.9pyppeteer版本:0.2.6pyee版本:8.2.2实现响应拦截的功能async def intercept_response(response): # resourceType = response.request.resourceType # print(resourceType) if 'get-list' in response.url: print(response.url) # print(原创 2022-01-19 18:27:10 · 2722 阅读 · 0 评论 -
爬虫中遇到POST请求中表单数据出现重复参数时的处理方式
如下图 form data中出现的参数 disclosureSubtype[] needFields[] ,出现了至少两次那么爬虫中模拟请求时如何使用呢:一下两种方式均已测试可用(使用的python测试)格式一data = [ ('disclosureSubtype[]', '9503-1004'), ('disclosureSubtype[]', '9504-2106'), ('page', '0'), ('companyCd', ''), ('isNewThree',.原创 2021-11-19 16:06:41 · 1399 阅读 · 2 评论 -
apscheduler 遇到的问题
报错信息:D:\AMyData\Codes\Python\MyBlog\blog_services\venv\lib\site-packages\apscheduler\util.py:95: PytzUsageWarning: The zone attribute is specific to pytz's interface; please migrate to a new time zone provider. For more details on how to do so, see https:原创 2021-11-07 19:10:28 · 8914 阅读 · 4 评论 -
SQLAlchemy建表 更新时间随时间戳自动更新
update_time = db.Column(db.TIMESTAMP, nullable=False, server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')) # 记录的更新时间如果想要根据当前时间戳更新生效 需要有这个属性server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')...原创 2021-10-15 09:51:53 · 1033 阅读 · 0 评论 -
python 字典 get() 参数问题
字典经常用 但是还是第一次遇到这个问题,看来时第一次用这个参数字典对象去get其中不存在的key 时 默认返回None 是default 参数的默认值 如下:dict().get('recordTimes', default=None)当我遇到不存在key 时不返回None 时咋办,第一感觉就是改 default对应的值但是 这个地方有个坑dict().get('recordTimes', default='我想要的值')这是会报错Traceback (most recent cal原创 2021-09-17 17:51:26 · 403 阅读 · 1 评论 -
使用pycharm 将代码转换大小写
写代码时一般都将常量名写成大写便于区分但是由于本人英语垃圾 只认识英文小写 所以命名时很难直接写出大写的常量由于懒就脑中浮现能不能使用工具将小写字母转换成大写,于是就查到了以下方法:一、选中要转换的字母二、点击pycharm的编辑菜单然后就得到了如下结果...原创 2021-06-29 16:45:54 · 7368 阅读 · 0 评论 -
利用openpyxl 将excel xlsx文件中数据转为python 字典格式数据
废话少说 直接上代码python 3.7.9import openpyxlxlsxPath = './test.xlsx'# 第一步打开工作簿wb = openpyxl.load_workbook(xlsxPath)# 第二步选取表单sheet = wb.active# 按行获取数据转换成列表rows_data = list(sheet.rows)# 获取表单的表头信息(第一行),也就是列表的第一个元素titles = [title.value for title in rows_原创 2021-06-23 10:50:07 · 1699 阅读 · 0 评论 -
局域网内搭建文件共享服务器(python 轻松实现)
昨天公司出了一个需求:要实现公司内部文件共享。起初的想法是,使用windows系统自带的共享设置,但是感觉来回设置有点麻烦,由于之前就想搭建一个文件共享服务器的想法,刚好碰上了,于是说干就干环境配置服务器:公司限制低配主机一台服务器系统:windows 7 x64(着急使用,没有重装系统)1、系统安装python3这个网上有安装教程,这里就不赘述了2、开启python 自带的HT...原创 2019-04-24 18:30:51 · 31093 阅读 · 5 评论 -
celery 异常 strategy = strategies[type_] KeyError: ‘celery_task.tasks1.filter_befor_save‘
使用的环境python: 3.7.9celery : 5.0.5异常信息:具体的报错原因暂时没找到,不过经历了半天的时间在网上找到了很多种方法,但是只有这一种有效当然是否是适合其他版本,我这里没有测试,你可以试试,我这只是自己的一个记录解决方法:在创建celery对象时 添加参数 include 参数celery对象创建 在 init.py 文件中from celery import Celeryfrom app import app # 配合flask一起使用的话原创 2021-03-23 10:39:28 · 2268 阅读 · 4 评论 -
xpath获取不包含某一子标签的a标签
获取子标签没有 img 标签的a标签xpath('//li//a[not(img)]')原创 2021-01-25 16:39:34 · 2709 阅读 · 1 评论 -
python 爬虫 自动识别相应编码
res = s.get(urlTemp, headers=headers, timeout=5)res.encoding = res.apparent_encoding# html_ = etree.HTML(res.content.decode('gbk'))print(res.text)原创 2021-01-25 10:43:31 · 565 阅读 · 0 评论 -
python 汉字转拼音 获取首字母
方式一、通过第三方库import pinyinprint(pinyin.get('汉字'))# 获取首字母,可将中文字符串转为中文列表 然后获取每一个汉字的拼音字符串取第一个然后拼接即可print(pinyin.get('汉'))[0]方式二、通过编码对比def single_get_first(unicode1): str1 = unicode1.encode('gbk') try: ord(str1) return str1.原创 2020-11-22 15:10:00 · 7519 阅读 · 3 评论 -
pymysql 异常 pymysql.err.InternalError: (1046, ‘No database selected‘)
在使用pymysql执行sql语句时出现异常 异常如下图其实报错信息已经指明 没有指定数据库 但是我还是没能理解其实在使用 pymysql 链接数据库操作表的的时候 需要在连接的时候 就指定数据库的当然 如果你是链接数库 执行建库 的操作 那你链接的时候知名数据库是不行的...原创 2020-07-21 11:13:28 · 7553 阅读 · 5 评论 -
sqlalchmy 插入数据时自动更新时间
方式一、代码中生成时间后自动插入到数据库class Task(BaseModel): __tablename__ = 'task_spiders' # 表名 nullable # 定义各字段 task_num = db.Column(db.Integer, autoincrement=True, primary_key=True, nullable=False, comment='任务ID/序号 自增 任务唯一标识') task_name = db.Column(db.S原创 2020-11-03 09:36:53 · 730 阅读 · 0 评论 -
pyppeteer 异常 RuntimeError: There is no current event loop in thread ‘Thread-3‘.
创建子线程 调用pyppeteer时 报错RuntimeError: There is no current event loop in thread 'Thread-3'.pyppeteer启动代码loop = asyncio.get_event_loop()loop.run_until_complete(main(name, password, keyword, isGetDefaultSKU)) # 将协程加入到事件循环looploop.close()解决办法...原创 2020-07-21 15:44:12 · 3896 阅读 · 1 评论 -
pyppeteer 异常 ValueError: signal only works in main thread
在flask 中创建子线程 调用 pyppeteer程序时 报错ValueError: signal only works in main thread直译过来解决办法创建浏览器对象时 添加三个字段'handleSIGINT':False,'handleSIGTERM': False,'handleSIGHUP': False,这里有别人的回答https://stackoverflow.com/questions/53679905/running-pypupete...原创 2020-07-21 15:35:55 · 3684 阅读 · 2 评论 -
Flask-SQLAlchemy创建模型时怎么给字段添加注释
原创 2020-07-20 15:33:51 · 1715 阅读 · 0 评论 -
flask 报错 sqlalchemy.exc.ArgumentError: Mapper mapped class BaseModel->base_model could not assemble
报错信息:sqlalchemy.exc.ArgumentError: Mapper mapped class BaseModel->base_model could not assemble any primary key columns for mapped table 'base_model'很基础的一个问题 定义模型类时 抽取出来了一个基类 但是没有标记 所以flask在 数据迁移的时候 误以为 这个基类也是一个数据表对应的模型类 进行解析出错解决办法在定义基类时 加入...原创 2020-07-20 15:08:49 · 2314 阅读 · 1 评论 -
爬虫返回数据 16进制编码问题
昨天分析爬虫接口时 发现返回数据 json转化时出错 经过检查发现问题是 json字符串中有 \x3E、\x2F这种编码字符串,开始解决思路就是把16进制转化为普通字符串结果不行原因编码问题 本来这些返回数据是需要前端js 去解析的\x 开头的16进制数据 是js的表示方法0x 开头 才是python 16进制的表示方法所以返回的数据不处理 直接用python 去解析会出现异常解决办法在 处理之前 先对返回的数据 进行编码格式处理res = response.content.de.原创 2020-07-02 12:04:46 · 2033 阅读 · 0 评论 -
python 压缩文件 zip
def FilesToZip(fps, zip_fp, delete=False): ''' 多文件打包成zip :param fps: [r'C:\1.txt', r'C:\2.txt', r'C:\3.txt'] 文件全路径的list :param zip_fp: r'C:\files.zip' :param delete: True 删除原文件 :return: ''' if len(fps)==0: raise Exception原创 2020-05-30 14:24:12 · 358 阅读 · 0 评论 -
pyppeteer.errors.NetworkError: Protocol error (Runtime.releaseObject): Cannot find context with spec
这个问题 网上记录似乎初期的少 还是面向谷歌才找到蛛丝马迹 还是国内的一个大佬的文章 真是是忍不住吐槽百度 国内的文章百度死活搜不到 人家国外的搜索引擎 都能看到你家内裤颜色了好了言归正传 多亏这位大佬的提示https://blog.csdn.net/qq_33850304/article/details/103042138虽然按照他的办法 我这边行不通 但是他提到原因可能是 因为pyppeteer的操作都是并发的,导致前一个请求页面还没加载完,就要回退,这就导致了报错:pyp...原创 2020-05-27 17:40:20 · 2510 阅读 · 3 评论 -
python3 输出 26个大小写英文字母
import stringwords1 = list(string.ascii_uppercase) # 26个大写英文字母print(words1)words2 = list(string.ascii_lowercase) # 26个小写英文字母print(words2)words3 = list(string.ascii_letters) # 52个小写和大写英文字母...原创 2020-04-21 17:41:37 · 7458 阅读 · 1 评论 -
python 操作微信云数据库
class DB(): def __init__(self): self.WECHAT_URL = 'https://api.weixin.qq.com/' self.APP_ID = 'wxc8e638b461ec4859' self.APP_SECRET = '5bcd735f03e86317a4811f4e3dab3134' ...原创 2020-03-11 11:29:18 · 1093 阅读 · 2 评论 -
pyppeteer 异常处理 ValueError: signal only works in main thread
程序里需要在一个县城里运行 pyppeteer 结果报了下边这个一场信息解决办法创建浏览器对象时加上这三个字段'handleSIGINT':False,'handleSIGTERM': False,'handleSIGHUP': False,参照:https://stackoverflow.com/questions/53679905/running-pypupete...原创 2019-12-26 17:43:15 · 2185 阅读 · 0 评论 -
python 导出 mysql表数据为 csv文件
# coding:utf8import win32apiimport win32conimport xlwtimport pymysqlimport oshost = '127.0.0.1'user = 'root'pwd = '123456'db = 'test'sql = 'select * from master_worker'sheet_name = '师...原创 2019-12-02 15:16:27 · 1242 阅读 · 0 评论 -
python functools.wraps装饰器模块
https://www.cnblogs.com/Neeo/p/8371826.html转载 2019-11-29 11:57:58 · 190 阅读 · 0 评论 -
JSON parse error: Unrecognized token 'telephone': was expecting 'null', 'true', 'false' or NaN;
做爬虫时候遇到了一个异常 很奇怪 就记录下完整异常{"timestamp":"2019-11-17T08:02:07.931+0000","status":400,"error":"Bad Request","message":"JSON parse error: Unrecognized token 'telephone': was expecting 'null', 'true', ...原创 2019-11-17 16:11:36 · 5407 阅读 · 0 评论 -
python pdf转txt
import pdfplumberpath = "pdf/21.pdf"with pdfplumber.open(path) as pdf: pages = pdf.pages for page in pages: with open('pdf/21.txt', 'a') as f: t = page.extract_text()...原创 2019-11-15 13:20:40 · 901 阅读 · 1 评论 -
python 获取指定目录下文件数量及文件列表
import globpath_file_number=glob.glob('./*.py')#或者指定文件下个数# path_file_number=glob.glob(pathname='*.py') #获取当前文件夹下个数print(path_file_number)print(len(path_file_number))...原创 2019-11-15 13:18:28 · 3580 阅读 · 0 评论 -
No module named 'PyQt5.QtWebEngineWidgets'
转载于https://blog.csdn.net/jenas999/article/details/82974811环境:python 3.6.5pyqt5出错提示:No module named 'PyQt5.QtWebEngineWidgets'原因:原文链接:https://www.riverbankcomputing.com/software/pyqt/downloa...原创 2019-11-08 08:56:07 · 465 阅读 · 0 评论 -
Python 批量递归给文件及文件夹重命名
将不想要的部分去除import osdef rename_file(file_dir): for root, dirs, files in os.walk(file_dir): print(1,root) #当前目录路径 print(2,dirs) #当前路径下所有子目录list print(3,files) #当前路径下所有非目...原创 2019-07-18 18:01:59 · 1369 阅读 · 1 评论 -
Scrapy框架-xpath用法
def parse_content(self,response): title = response.xpath("//h2[@id='activity-name']/text()").extract_first() print(title.strip())response.xpath("//h2[@id='activity-name']/text()"...原创 2019-07-25 11:45:13 · 715 阅读 · 0 评论 -
Python 制作词云
from wordcloud import WordCloudimport PIL.Image as imageimport numpy as npimport re# 1、读取文本with open("C:\\Users\\Administrator\\Desktop\\ji.txt","r") as fp: text = fp.read() # 2、整理文本 y...原创 2019-07-21 09:10:26 · 157 阅读 · 0 评论 -
pyinstaller打包exe出现 selenium 黑窗口问题
修改 Python\Python36\Lib\site-packages\selenium\webdriver\common 文件夹下的 service.py 文件的源码如下图所示:定位到代码的第 75行,在末尾添加一行参数,然后保存即可...原创 2019-08-01 11:31:51 · 1676 阅读 · 0 评论 -
Python 批量操作文件,批量解压缩
# 批量操作文件,批量解压缩import osimport shutilimport rarfileimport redef main(target_path): # print(os.listdir(target_path)) for root, dirs, files in os.walk(target_path): now_dir = root ...原创 2019-08-08 17:53:41 · 3118 阅读 · 0 评论 -
Python中浅拷贝,深拷贝的区别
import copy# 元组q = (1,)w = copy.copy(q)e = copy.deepcopy(q)print("元组:",id(q), id(w), id(e))# 字典q1 = {"a":1}w1 = copy.copy(q1)e1 = copy.deepcopy(q1)print("字典:",id(q1), id(w1), id(e1))...原创 2019-08-14 17:56:48 · 114 阅读 · 0 评论 -
python 报警声音的实现
windows 上import winsoundduration = 1000 # millisecondfreq = 440 # Hzwinsound.Beep(freq, duration)其中,freq 是频率(以赫兹为单位),而duration 是持续时间 单位是 毫秒。https://cloud.tencent.com/developer/ask/53181 待整...原创 2019-08-31 11:04:20 · 11670 阅读 · 1 评论 -
CentOS 7安装配置Python3.7环境
CentOS 7 已经默认安装了python2.7.5查看系统默认的 Python 相关软连接指向python python2 python2.7 最终都是指向Python2.7 版本所以安装过Python3.x 后需要注意软连接一、安装相关依赖yum -y groupinstall "Development tools"yum -y install zlib-devel bz...原创 2019-09-02 18:04:14 · 269 阅读 · 0 评论 -
阿里云Linix上 python脚本 发邮件发送不出去问题
脚本在本地发送没问题,但是同样的系统环境,在阿里云上就是发送不出去报错信息只能看出 是连接的问题原因可能是阿里云 把 你使用的哪个端口封了解决:修改连接部分代码换端口号换成ssl方式连接(这个不知到是不是原因)smtplib.SMTP_SSL(mail_host, 465) # _*_coding:utf-8 _*_#邮件服务封装import smtplibf...原创 2019-09-06 08:54:58 · 1394 阅读 · 0 评论