Python使用urllib2模块抓取HTML页面资源的实例分享


先把要抓取的网络地址列在单独的list文件中

http://www.gimoo.net/article/83440.html
http://www.gimoo.net/article/83437.html
http://www.gimoo.net/article/83430.html
http://www.gimoo.net/article/83449.html

然后我们来看程序操作,代码如下:

#!/usr/bin/python

import os
import sys
import urllib2
import re

def Cdown_data(fileurl, fpath, dpath):
 if not os.path.exists(dpath):
  os.makedirs(dpath)
 try:
  getfile = urllib2.urlopen(fileurl) 
  data = getfile.read()
  f = open(fpath, 'w')
  f.write(data)
  f.close()
 except:
 print 

with open('u1.list') as lines:
 for line in lines:
  URI = line.strip()
  if '?' and '%' in URI:
   continue
 elif URI.count('/') == 2:
   continue
  elif URI.count('/') > 2:
   #print URI,URI.count('/')
  try:
    dirpath = URI.rpartition('/')[0].split('//')[1]
    #filepath = URI.split('//')[1].split('/')[1]
    filepath = URI.split('//')[1]
   if filepath:
     print URI,filepath,dirpath
     Cdown_data(URI, filepath, dirpath)
   except:
    print URI,'error'

解析Mac OS下部署Pyhton的Django框架项目的过程
一、安装软件包并创建项目$sudopipinstalldjango$sudopython-c"importdjango;printdjango.VERSION"(1,7,0,'final',0)$sudodjango-adminstartprojectcmdb#创建项目$sudodjango-adminstartappcmdb#创

Python中函数参数设置及使用的学习笔记
一、参数和共享引用:In[56]:defchanger(a,b):....:a=2....:b[0]='spam'....:In[57]:X=1In[59]:L=[1,2]In[60]:changer(X,L)In[61]:X,LOut[61]:(1,['spam',2])函数参数是赋值得来,在调用时通

Python中使用插入排序算法的简单分析与代码示例
问题描述将一组随机排列的数字重新按照从小到大的顺序排列。插入算法每次从数组中取一个数字,与现有数字比较并插入适当位置。如此重复,每次