![](https://img-blog.csdnimg.cn/20201014180756918.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
python基础
总想转行
菜鸡一个 写的文章很粗糙,用作自我记录 如果有帮助到你我很开心 有错请勿喷 好好说
展开
-
python程序打包成exe执行程序并执行
参考:https://www.cnblogs.com/valorchang/p/11357358.html1.pyinstaller工具pyinstaller -F -i text.ico -w mytext.py #这里的-w是用于打包成独立文件,不能进行交互pyinstaller -F -i text.ico mytext.py #打包出来的exe中类似input的交互函数会生效2.py2exe开始执行exe文件报错:1.找不到requests模块解决:1)先查看本地是否有原创 2021-07-14 16:18:19 · 194 阅读 · 0 评论 -
python中的线程和进程使用
线程基础import threading #线程库import timeimport queueclass myThread(threading.Thread): #继承线程库里面的Thread类 def __init__(self,threadname,threadid,waittime): '''线程名,线程id,等待执行时间''' threading.Thread.__init__(self) #必须要有这句 没有的话就报错 se原创 2021-04-06 16:11:45 · 136 阅读 · 0 评论 -
unittest模块使用
首先要知道,一个完整的单元测试流程:完整单元测试流程:1.写好一个完整的TestCase类2.多个TestCase 由TestLoder被加载到TestSuite里面, TestSuite也可以嵌套TestSuite3.由TextTestRunner来执行TestSuite,测试的结果保存在TextTestResult中 TestFixture指的是环境准备和恢复testcase类完善1.确定需要测试的模块#需要测试下面这两个方法def myadd(a,b): return原创 2021-04-06 11:35:34 · 150 阅读 · 0 评论 -
python中多种调用其他指令的方式
# -*- coding:UTF-8 -*-import sys,osimport subprocessvaluea="bob"valueb="say some thing!!"command="echo 'hello world'"os.system(command) #返回值为0则成功with os.popen(command,'r') as a: #返回值是文件对象 print(a.read())a=os.popen(command,'r')print(a.read()原创 2021-03-24 10:56:01 · 90 阅读 · 0 评论 -
selenium实现免密登录的几种方式
1.设置数据目录免登陆通过数据目录免登陆:设置后,driver.get时先手动输入密码登录,再次打开网页时就不需要输入密码了2.通过获取cookie免登陆https://www.cnblogs.com/blogofzxf/p/10566322.html例:将cookie存放在文件内,加载文件失败时,执行自动登录操作并保存cookie,这样就不用每次都登录了直接执行:loadcookies()...原创 2021-02-24 16:04:02 · 1901 阅读 · 0 评论 -
selenium基础使用
from selenium import webdriverimport time#声明浏览器对象chromeDriver = 'C:\driver\chrome\chromedriver.exe'driver=webdriver.Chrome(chromeDriver)#访问页面(html源码)url="https://www.qidian.com/"driver.get(url)html=driver.page_source#拖拽操作from selenium.webdrive原创 2021-02-22 11:28:30 · 98 阅读 · 0 评论 -
openpyxl读取和写入excel文件
采用openpyxl处理# -*- coding:UTF-8 -*-import openpyxl# import pandasdef readwb(): '''读取已经存在的excel文件内容''' xlsx = "./userinfo.xlsx" wb = openpyxl.load_workbook(xlsx) print(wb.worksheets) #读取所有的excel中存在工作表 ##第一种遍历方式 sheet=wb['入职名单']原创 2021-02-01 15:13:53 · 410 阅读 · 0 评论 -
python处理文件是否可读
if os.path.exists('./a.txt'): print("file exists!") os.remove('a.txt')else: print("file not exists!")path='./mount.xml'if not os.access(path, os.R_OK): # R_OK可读;X_OK 可执行;os.W_OK 文件写权限 print("file not exists or can't read")...原创 2021-01-28 17:14:00 · 163 阅读 · 0 评论 -
python将jenkins获取的时间转换为正常的年月日显示
from datetime import datetime#将jenkinspipeline中的currentBuild.startTimeInMillis转换为日常使用的时间格式:#a=int(currentBuild.startTimeInMillis)a=1611737708290b=datetime.fromtimestamp(a/1000)c=datetime.fromtimestamp(a/1000).strftime('%Y-%m-%d %H:%M:%S')print(b)pri原创 2021-01-28 17:07:10 · 940 阅读 · 0 评论 -
python循环字典的两种方式
import sys,osa={"package_server1_url":"/mountpath/server1/path", "package_server1_mount_cmd":"mout 172.*.*.*/server1 /mountpath/server1/path", "package_server2_url":"/mountpath/server2/path", "package_server2_mount_cmd":"mout 172.*.*.*/server1 /原创 2021-01-28 17:00:23 · 558 阅读 · 0 评论 -
python程序调用shell指令的多种方法
# -*- coding:UTF-8 -*-import sys,osimport subprocessvaluea="bob"valueb="say some thing!!"command="echo 'hello world'"#第一种:os.system()os.system(command) #返回值为0则成功#第二种:os.popen()with os.popen(command,'r') as a: #返回值是文件对象 print(a.read())a=os.p原创 2021-01-28 16:50:47 · 71 阅读 · 0 评论 -
python连接数据库:pymsql模块基础操作
import pymysql,os,sysmydb=pymysql.connect(host='localhost',user='root',password='',db='mydb',charset='utf8')cursor=mydb.cursor()try: try: data=(9,'newname','python5',34) sql="insert into mytable values(%d,'%s','%s',%d);" % (data)原创 2021-01-26 17:03:21 · 75 阅读 · 0 评论 -
python下载图片的三种方式
img_url是图片的链接,pic_id+’.jpg’为图片名字(可以指定完整路径)######正常方式下载图片####data=requests.get(img_url).contentwith open(pic_id+'.jpg','wb') as file: file.write(data) #####通过字节流下载图片####with requests.get(img_url,stream=True) as imgbyte: #一直打开图片流 with open(pic_id+'.原创 2021-01-26 16:55:57 · 805 阅读 · 0 评论 -
scrapy爬虫框架基础使用知识
1.确保安装了myscrapy模块(爬虫专用)2.scrapy startproject project_name #创建项目名为project_name的爬出项目3.用编译工具打开爬虫项目,目录有:spiders:在这下面操作爬虫init.py #默认的 不动**.py #自己定义的操作数据的文件items.py #定义需要爬取的数据settings.py:爬虫配置文件middlewares.py #用来对spider返回的item列表进行数据的保存等操作,可以写入文件或保存到数原创 2021-01-26 16:29:13 · 65 阅读 · 0 评论 -
html页面解析类:xml(用的是etree下的parse)
参考:https://www.jb51.net/article/63780.htmtree = ET.parse(“country.xml”)import xml.etree.ElementTree as ET tree = ET.parse("country.xml")root = tree.getroot()root.tag for child in root: print child.tag, "---", child.attrib 或者:try: import xml原创 2021-01-26 17:01:51 · 282 阅读 · 0 评论 -
html页面解析类:lxml(用的是etree下面的xpath)
html=etree.HTML(content)#-*-coding:UTF-8-*-from lxml import etree #xpath是etree内的模块import sysreload(sys)sys.setdefaultencoding('utf8')file=open('a.html','r')content=file.read()html=etree.HTML(content) #将html内容转换为树状结构的可访问内容file.close()####开始读取h原创 2021-01-25 17:47:11 · 281 阅读 · 0 评论 -
html页面解析类:re模块解析
re模块可以进行替换、匹配、分割等操作 #re模块 正则表达式:匹配 检索 替换 分割用import sys,os,re#####查询 search() match() findall() finditem()######res=re.search("[0-9]",'abscd23') #在后面的字符串查找数字 这里是用的re模块的函数print(res) #<re.Match object; span=(5, 6), match='2'>print(res.group原创 2021-01-25 17:42:25 · 541 阅读 · 0 评论 -
html页面解析类:BeautifulSoup基础使用
bs4:可以解析为lxml格式,也可以解析为html格式基础使用#-*-coding:UTF-8-*-from bs4 import BeautifulSoupimport os,sysimport json# reload(sys)# sys.setdefaultencoding('utf8')if os.path.exists('a.html'): print "file not exist!!"# excelfile='C:\Users\W9005357\Desktop\g原创 2021-01-25 17:36:51 · 207 阅读 · 0 评论 -
selenium库:页面获取类
具体请百度原创 2021-01-25 17:11:09 · 88 阅读 · 0 评论 -
requests库:页面获取类
简单使用import requestsimport reurl="https://shimo.im/docs/CHQt86xJvgp3R8yp"headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'} #伪装一个浏览器用户resp=requests.get(url,headers原创 2021-01-25 16:43:46 · 144 阅读 · 0 评论 -
urllib库:页面获取类
使用的两种方式:1.urllib.request.urlopen()方法直接使用urllib.reuqest.urlopen(url)2.urllib.request.Request类:将url变为一个对象进行操作urlR=urllib.request.Request(url)urllib.request.urlopen(urlR) #打开的是url的对象urllib实例操作一:urllib.error的三种异常处理方式分别有HTTPError 和URLError两种子类import ur原创 2021-01-25 15:35:47 · 128 阅读 · 0 评论