您的位置:首页 > 编程语言 > Python开发

So cute are you python 15

2013-10-31 21:57 447 查看
Python 创建文件:

1) os.mknod("test.txt") 创建空文件

2) open("test.txt",w) 直接打开一个文件,如果文件不存在则创建文件

创建目录:

os.mkdir("file") 创建目录

复制文件:

shutil.copyfile("oldfile","newfile") oldfile和newfile都只能是文件

shutil.copy("oldfile","newfile") oldfile只能是文件夹,newfile可以是文件,也可以是目标目录

复制文件夹:

shutil.copytree("olddir","newdir") olddir和newdir都只能是目录,且newdir必须不存在

重命名文件(目录)

os.rename("oldname","newname") 文件或目录都是使用这条命令

移动文件(目录)

shutil.move("oldpos","newpos")

删除文件

os.remove("file")

删除目录

os.rmdir("dir") 只能删除空目录

shutil.rmtree("dir") 空目录、有内容的目录都可以删

转换目录

os.chdir("path") 换路径

判断目标

os.path.exists("goal") 判断目标是否存在

os.path.isdir("goal") 判断目标是否目录

os.path.isfile("goal") 判断目标是否文件

网络上抓取 a 标签 分析URL:

代码:

#!/usr/bin/evn python
#coding:utf-8
#FileNamesearch_deth.py
#Function:Tying to found some message from internet.
#History:31-10-2013
import bs4
from bs4 import BeautifulSoup
import urllib

class bea_Demo:
global get
def __init__(self,url):
self.url=url

def run(self):
print 'ok'
#getit(self)

def getit(self):
print 'Found All a tags on one page..'
ss=urllib.urlopen(self.url)
page=ss.read()
soup = BeautifulSoup(page)
lists=soup.findAll('a')
list2=[]
for i in lists:
s=str(i).find('href')
ss=str(i)[s+6:]
ss1=ss.find('"')
ssa=ss[:ss1]
print ssa,'Title:',i.string

if __name__=='__main__':
print '开始搜索。。。\r\n'
b=bea_Demo('http://www.360.cn')
b.run()
b.getit()


结果:

$ python search_deth.py
开始搜索。。。

ok
Found All a tags on one page.. http://v.360.cn/android Title: None
#nogo Title: None http://www.360.cn Title: None http://www.360.cn Title: 首页
ass= Title: 电脑软件
ass= Title: 手机软件 http://wifi.360.cn/?source=360cn Title: 随身WiFi http://t.360.cn/ Title: 特供机 http://fuwu.360.cn Title: 理赔举报
ass= Title: 个人服务
ass= Title: 企业服务 http://wan.360.cn Title: 游戏 http://bbs.360safe.com/ Title: 论坛 http://www.360.cn/weishi/index.html Title: 安全卫士 http://sd.360.cn/ Title: 杀毒 http://se.360.cn/ Title: 安全浏览器 http://chrome.360.cn/ Title: 极速浏览器 http://www.360.cn/jijiuxiang/index.html Title: 系统急救箱 http://yunpan.360.cn/ Title: 云盘 http://zhuomian.360.cn/ Title: 安全桌面 http://www.360.cn/safebox/index.html Title: 游戏保险箱 http://bizhi.360.cn/ Title: 壁纸 http://360game.360.cn/ Title: 游戏大厅 http://b.360.cn/safe Title: 企业版 http://yasuo.360.cn/ Title: 压缩 http://www.360.cn/qudongdashi/index.html Title: 驱动大师 http://www.ludashi.com/ Title: 鲁大师 http://shouji.360.cn/index.html Title: 手机卫士 http://www.360.cn/shoujizhushou/index.html Title: 手机助手 http://shouji.360.cn/pop/360batterysaver/index.html Title: 省电王 http://mhzm.360.cn/ Title: 美化桌面 http://mse.360.cn/ Title: 手机浏览器 http://shouji.360.cn/pop/360sysopt/index.html Title: 优化大师 http://txl.360.cn/ Title: 安全通讯录 http://mobile.360.cn/index.html Title: 手机桌面 http://v.360.cn/android Title: 360影视大全 http://yunpan.360.cn/resource/html/mobile.html Title: 手机云盘

2.把链接存入文件:

#!/usr/bin/evn python
#coding:utf-8
#FileNamesearch_deth0.py
#Function:Tying to found some message from internet and write the links to files
#History:31-10-2013
import bs4
from bs4 import BeautifulSoup
import urllib
import os

class bea_Demo:
global get
def __init__(self,url):
self.url=url

def run(self):
print 'ok'
#getit(self)

def getit(self):
print 'Found All a tags on one page..'
ss=urllib.urlopen(self.url)
page=ss.read()
soup = BeautifulSoup(page)
lists=soup.findAll('a')
list2=[]
if os.path.isfile('test.txt'):
print 'File exitsts!!!'
else:
print 'Created an file named text.txt'
os.mknod("test.txt")
f=open('test.txt','w')

for i in lists:
s=str(i).find('href')
ss=str(i)[s+6:]
ss1=ss.find('"')
ssa=ss[:ss1]
print ssa,'Title:',i.string
f.write(ssa)
#f.write(i.string)

f.close()

if __name__=='__main__':
print '开始搜索。。。\r\n'
b=bea_Demo('http://www.baidu.com')
b.run()
b.getit()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: