摘要:
os.path模块 basename('文件路径') 去掉目录路径,返回fname文件名 1 import os 2 os.path.basename('/Volumes/1.mp4') #输出('1.mp4') dirname('文件路径') 去掉文件名,返回目录路径 1 import os 2 os.path.dirname('/Volumes/1.mp4') #输出('/Volumes') splitdrive('文件路径') 返回(drivername,fpath)元组 vi 阅读全文
摘要:
seek():移动文件读取指针到指定位置 tell():返回文件读取指针的位置 seek()的三种模式: (1)f.seek(p,0) 移动当文件第p个字节处,绝对位置 (2)f.seek(p,1) 移动到相对于当前位置之后的p个字节 (3)f.seek(p,2) 移动到相对文章尾之后的p个字节 c 阅读全文
摘要:
decode 解码encode 转码unicode是一种编码,具体可以百度搜# coding: UTF-8 u = u'汉'print repr(u) # u'\u6c49's = u.encode('UTF-8')print repr(s) # '\xe6\xb1\x89'u2 = s.decode('UTF-8')print repr(u2) # u'\u6c49' # 对unicode进行解码是错误的# s2 = u.decode('UTF-8')# 同样,对str进行编码也是 阅读全文
摘要:
Requests:Python HTTP Module学习笔记(一)在学习用python写爬虫的时候用到了Requests这个Http网络库,这个库简单好用并且功能强大,完全可以代替python的标准库urllib2。在学习的同时把我的学习笔记记录下来,资料基本上都是从Requests官网翻译过来的,欢迎指出有错误或者有表述的不准确的地方。1.介绍Requests: HTTP for Humans一句话:为地球人准备的网络库python的标准库urllib2已经提供了大部分你所需要的HTTP功能了,为什么还需要Requests库来提供相同的功能呢?因为urllib2的API比较零散,它们编写的 阅读全文
摘要:
在上一篇日志中对Requests做了一个整体的介绍,接来下再介绍一些高级的用法,主要资料还是翻译自官网的文档,如有错漏,欢迎指正。参考资料:http://docs.python-requests.org/en/latest/user/advanced/会话对象(Session Objects)一个请求传递的参数,在另一个请求是无效的,不过会话对象允许跨请求的保存参数,还可以从会话实例中保存所有请求的cookies数据。下面介绍一下在Requests中会话对象主要的API。跨请求的保存cookies数据:s = requests.Session()s.get('http://httpbi 阅读全文
摘要:
import osimport os.pathrootdir = “d:\data” # 指明被遍历的文件夹for parent,dirnames,filenames in os.walk(rootdir): #三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字 for dirname in dirnames: #输出文件夹信息 print "parent is:" + parent print "dirname is"... 阅读全文
摘要:
之前一直被datetime,date,time弄的有点乱,可能是因为看文档每太看明白,找到了两篇文章供大家阅读都是转载的,其中有些名词这里解释一下:世界协调时间(Universal Time Coordinated,UTC) GPS 系统中有两种时间区分,一为UTC,另一为LT(地方时)两者的区别为时区不同,UTC就是0时区的时间,地方时为本地时间,如北京为早上八点(东八区),UTC时间就为零点,时间比北京时晚八小时,以此计算即可 尊重作者:下面文章转自:http://hi.baidu.com/_yuan0518/blog/item/5ed3d0372444532d0a55a9ac.html. 阅读全文
摘要:
python datetime 时间日期处理小结转载请注明出处:http://hi.baidu.com/leejun_2005/blog/item/47f340f1a85b5cb3a50f5232.htmlfrom:http://hi.baidu.com/wind_stay/blog/item/e8cddc37726843d6a2cc2bfd.htmlimport datetime, calendar #-*-coding:utf-8-*- #1、返回昨天日期def getYesterday(): today=datetime.date.today() oneday=datetime.ti.. 阅读全文
摘要:
Python中time模块详解发表于2011年5月5日 12:58 a.m. 位于分类我爱Python在平常的代码中,我们常常需要与时间打交道。在Python中,与时间处理有关的模块就包括:time,datetime以及calendar。这篇文章,主要讲解time模块。在开始之前,首先要说明这几点:在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素。由于Python的time模块实现主要调用C库,所以各个平台可能有所不同。UTC(Coordinated Universal Time,世界协调时)亦即格林威治天文时间,. 阅读全文
摘要:
#coding:gbkfrom selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support.ui import WebDriverWaitimport timedriver=webdriver.Chrome()driver.get('http://www.baidu.com')driver.find_element_by_id('lb').click()#driver.find_element_by_id(' 阅读全文
摘要:
selenium-webdriver(python)--cookie处理driver.get_cookies()获得cookie信息add_cookie(cookie_dict)向cookie添加会话信息delete_cookie(name)删除特定(部分)的cookiedelete_all_cookies()删除所有cookie通过webdriver操作cookie是一件非常有意思的事儿,有时候我们需要了解浏览器中是否存在了某个cookie信息,webdriver可以帮助我们读取、添加,删除cookie信息。打印cookie信息#coding=utf-8fromseleniumimportw 阅读全文
摘要:
1.通过类class获取 比如如下代码 This heading is very important. This paragraph is very important. This paragraph is a very important ... 阅读全文
摘要:
from selenium import webdriverfrom selenium.webdriver.common.keys import Keysimport timedriver=webdriver.Chrome()driver.get('http://www.baidu.com')driver.find_element_by_id('lb').click()#driver.find_element_by_id('TANGRAM__PSP_10__unameLoginLink').click()time.sleep(3)driver.f 阅读全文
摘要:
先列出selenium所有方法,然后挨个使用!说明add_cookieapplication_cachebackcapabilitiesclosecommand_executorcreate_web_elementcurrent_urlcurrent_window_handledelete_all_cookiesdelete_cookiedesired_capabilitieserror_handlerexecuteexecute_async_scriptexecute_scriptfind_elementfind_element_by_class_namefind_element_by_cs 阅读全文
摘要:
易迅的登录方法,因为页面有很多iframe的内置框架,需要先逐级定位到登录元素所在的iframe才行使用方法switch_to_frame('id-name')from selenium import webdriverfrom selenium.webdriver.common.keys import Keysdriver = webdriver.Chrome()driver.get("http://item.yixun.com/item-712354.html?YTAG=3.21012000")driver.find_element_by_id(&quo 阅读全文
摘要:
1、下载pyinstaller目前pyinstaller支持的python版本为2.3-2.7,可以到http://www.pyinstaller.org/官网下载。2、安装下载完成后,解压即可。3、使用方法使用也非常的简单,cmd下进入解压出来的目录,执行如下命令。python pyinstaller.py [opts] yourprogram.py主要选项包括:-F, --onefile 打包成一个exe文件。-D, --onedir 创建一个目录,包含exe文件,但会依赖很多文件(默认选项)。-c, --console, --nowindowed 使用控制台,无界面(默认)-w... 阅读全文
摘要:
浏览器页面跳转方法记录:from selenium import webdriverimport timebrowser = webdriver.Chrome()first_url='http://www.baidu.com'browser.find_element_by_xpath('//div/div/div/ul/li[1]/strong/a').click()browser.switch_to_window(browser.window_handles[0])browser.title #第一个页面browser.switch_to_window(bro 阅读全文
摘要:
python 的内嵌time模板翻译及说明 一、简介 time模块提供各种操作时间的函数 说明:一般有两种表示时间的方式: 第一种是时间戳的方式(相对于1970.1.1 00:00:00以秒计算的偏移量),时间戳是惟一的 第二种以数组的形式表示即(struct_time),共有九个元素,分别表示,同一个时间戳的struct_time会因为时区不同而不同 year (four digits, e.g. 1998) month (1-12) day (1-31) hours (0-23) minutes (0-59) seconds (0-59) weekday (0-6, Monday is 0 阅读全文
摘要:
lambda一句话函数:ff=lambda x,y:x+y#给ff传两个参数,就会执行lambda定义的x+yprint ff(2,3) #打印出 5python中的reduce内建函数是一个二元操作函数,他用来将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给reduce中的函数 func()(必须是一个二元操作函数)先对集合中的第1,2个数据进行操作,得到的结果再与第三个数据用func()函数运算,最后得到一个结果。如:先定义一个二元操作函数def f(x,y): return x+y reduce(f,(1,2,3,4,5))lambda和reduce结合起来使用:... 阅读全文
摘要:
1.break用法from math import sqrtfor i in range(91,80,-1): root=sqrt(i) if root==int(root): print 'shi 81`````' break else: print 'bushi bushi '#5.6列表推导式,轻量级循环[(x,y) for x in str(range(1,5)) for y in 'python123' if x==y]输出:[('1', '1'), ('2', '2'), 阅读全文