
#!/usr/bin/env python這只是最簡(jiǎn)單的功能,我們要做的要比這個(gè)復雜一些
# -*- coding: UTF-8 -*-
__author__ = '217小月月坑'
# os是系統庫,引入這個(gè)庫可以使用系統的命令行
import os
# 在當前目錄下創(chuàng )建文件,haha是目錄名
os.mkdir('haha')
# 在指定的目錄下創(chuàng )建文件,r''使字符轉義,hehe是目錄名
os.mkdir(r'/home/ym/hehe')
#!/uer/bin/env python
# -*- coding: UTF-8 -*-
__author__ = '217小月月坑'
'''
獲取網(wǎng)頁(yè)源碼中的相冊名字并用這個(gè)名字創(chuàng )建文件夾
'''
import urllib2
import re
import os
url = 'http://product.yesky.com/more/506001_31372_photograph_1.shtml'
user_agent = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:40.0) Gecko/20100101 Firefox/40.0'
headers = {'User-Agent':user_agent}
try:
request = urllib2.Request(url,headers=headers)
response = urllib2.urlopen(request)
conents = response.read().decode("gbk")
# RE獲取相冊首頁(yè)和相冊名字
pattern = re.compile(r'<dt><a href="(.*?)" title="(.*?)"',re.S)
items = re.findall(pattern,conents)
for item in items:
print item[0],item[1]
# 設置文件夾的路徑,作為參數傳給mkdir函數使用,item[1]是相冊名字
path = r'/home/ym/test/'+item[1]
# 創(chuàng )建文件夾
os.mkdir(path)
except urllib2.URLError,e:
if hasattr(e,"code"):
print e.code
if hasattr(e,"reason"):
print e.reason
聯(lián)系客服