1 #思路 : 利用beautiful 省去了正则这个麻烦事,把页面搞出来然后提取js,css,img ,提取命令使用getopt 很方便,使用前需要确保已经安装了beautiful soup,如没有安#装请 到 http://www.crummy.com/software/BeautifulSoup/ 下载
  2 from bs4 import BeautifulSoup
  3 import urllib, urllib2,time
  4 import sys,os
  5 import getopt
  6 reload(sys)  
  7 sys.setdefaultencoding("utf-8")
  8 
  9 #set default value 
 10 clock_time = 60
 11 target_url = "http://m.sohu.com"
 12 target_lib = "/tmp/backup"
 13 
 14 def usage() :
 15     print "simple like this :"
 16     print "main.py -d 60 -u http://m.sohu.com -o \tmp\backup"
 17 
 18 def getHtml(target_url,target_lib,time) :
 19     response = urllib.urlopen(target_url)
 20     Html= response.read()
 21     target_lib=target_lib+'/'+time
 22     os.makedirs(target_lib)
 23     #save html
 24     print target_lib
 25     try :
 26         f = open(target_lib+"/index.html","w")
 27         f.write(Html)
 28         f.close()
 29         print "save index.html ok!"
 30     except Exception,e:
 31         print str(e)
 32     
 33     # save picture     
 34     os.makedirs(target_lib+"/images")
 35     soup = BeautifulSoup(Html)
 36     f=soup.find_all('img')
 37     if f != None :
 38         for i in f :
 39             pic_url=i.get('src')
 40             response = urllib.urlopen(pic_url)
 41             pic_url=pic_url.split('/')
 42             pic= response.read()
 43             try :
 44                 f = open(target_lib+"/images/"+pic_url[-1],"wb")
 45                 f.write(pic)
 46                 f.close()
 47             except Exception,e :
 48                 print str(e)
 49                            
 50     print "save picture ok!"
 51     
 52     #save js 
 53     os.makedirs(target_lib+"/js")
 54     f=soup.find_all('script')
 55     noName=0
 56     if f != None :
 57         for i in f :
 58             if i.get('src')!=None :
 59                 js_url=i.get('src')
 60                 response = urllib.urlopen(js_url)
 61                 js_url=js_url.split('/')
 62                 js= response.read()
 63                 try :
 64                     f = open(target_lib+"/js/"+js_url[-1],"w")
 65                     f.write(js)
 66                     f.close()
 67                 except Exception,e :
 68                     print str(e)
 69             else :  # js 可以嵌入在文档里 保存为wuming
 70                 f = open(target_lib+"/js/"+"wuming"+str(noName)+".js","w")
 71                 noName+=1
 72                 f.write(i.string)
 73                 f.close()
 74     print "save js ok!"    
 75     
 76     #save css
 77     os.makedirs(target_lib+"/css")
 78     f=soup.find_all('link')
 79     if f != None :
 80             for i in f :
 81                 if i.get('type') != None and i.get('type') == "text/css" :
 82                     css_url=i.get('href')
 83                     response = urllib.urlopen(css_url)
 84                     css_url=css_url.split('/')
 85                     css= response.read()
 86                     try :
 87                         f = open(target_lib+"/css/"+css_url[-1],"w")
 88                         f.write(css)
 89                         f.close()
 90                     except Exception,e :
 91                         print str(e)
 92     print "save css ok!"
 93     
 94 def main() :
 95     global clock_time
 96     global target_url
 97     global target_lib
 98     
 99     if not len(sys.argv[1:]) :
         usage()
     try :
         opts,args = getopt.getopt(sys.argv[1:], "d:u:o:",[])
     except getopt.GetoptError as err :
         print str(err) 
         usage()
         
     for o,a in opts :
         if o in ("-d") :
             clock_time = a
         if o in ("-u") :
             target_url = a
         if o in ("-o") :
             target_lib = a
     
     lastTime = int(time.time())
     timeArray = time.localtime(lastTime)
     otherStyleTime = time.strftime("%Y%m%d%H%M", timeArray)    
     getHtml(target_url,target_lib,otherStyleTime)
     
     while True :
         nowTime=int(time.time())
         if nowTime - lastTime >= 60 :
             lastTime=nowTime
             timeArray = time.localtime(nowTime)
             otherStyleTime = time.strftime("%Y%m%d%H%M", timeArray)            
             getHtml(target_url,target_lib,otherStyleTime)     
             print "update at time" + otherStyleTime
 if __name__=="__main__" :
     main() 

py 爬取页面http://m.sohu.com 并存储的更多相关文章

  1. [实战演练]python3使用requests模块爬取页面内容

    本文摘要: 1.安装pip 2.安装requests模块 3.安装beautifulsoup4 4.requests模块浅析 + 发送请求 + 传递URL参数 + 响应内容 + 获取网页编码 + 获取 ...

  2. MinerHtmlThread.java 爬取页面线程

    MinerHtmlThread.java 爬取页面线程 package com.iteye.injavawetrust.miner; import org.apache.commons.logging ...

  3. 【java】使用URL和CookieManager爬取页面的验证码和cookie并保存

    使用java的net包和io包下的几个工具爬取页面的验证码图片并保存到本地. 然后可以把获取的cookie保存下来,做进一步处理.比如通过识别验证码,进一步使用验证码和用户名,密码,保存下来的cook ...

  4. scrapy中使用selenium来爬取页面

    scrapy中使用selenium来爬取页面 from selenium import webdriver from scrapy.http.response.html import HtmlResp ...

  5. 爬取豆瓣电影TOP 250的电影存储到mongodb中

    爬取豆瓣电影TOP 250的电影存储到mongodb中 1.创建项目sp1 PS D:\scrapy> scrapy.exe startproject douban 2.创建一个爬虫 PS D: ...

  6. py爬取英文文档学习单词

    最近开始看一些整本整本的英文典籍,虽然能看个大概,但是作为四级都没过的我来说还是有些吃力,总还有一部分很关键的单词影响我对句子的理解,因为看的是纸质的,所以查询也很不方便,于是想来个突击,我想把程序单 ...

  7. python 爬虫之requests爬取页面图片的url,并将图片下载到本地

    大家好我叫hardy 需求:爬取某个页面,并把该页面的图片下载到本地 思考: img标签一个有多少种类型的src值?四种:1.以http开头的网络链接.2.以“//”开头网络地址.3.以“/”开头绝对 ...

  8. python爬取豌豆荚中的详细信息并存储到SQL Server中

    买了本书<精通Python网络爬虫>,看完了第6章,我感觉我好像可以干点什么:学的不多,其中的笔记我放到了GitHub上:https://github.com/NSGUF/PythonLe ...

  9. Python 爬取美女图片,分目录多级存储

    最近有个需求:下载https://mm.meiji2.com/网站的图片. 所以简单研究了一下爬虫. 在此整理一下结果,一为自己记录,二给后人一些方向. 爬取结果如图:   整体研究周期 2-3 天, ...

随机推荐

  1. PMP项目管理学习笔记引言(1)——为啥要取得认证?

    (一)为啥要取得认证? 如果你参与过很多项目,就会发现,你总是在周而复始地面对同样的一些问题.一些常见的问题目前已经有了通用解决方案.经过多年的实战,项目经理已们已经掌握了很多应验教训,而通过PMP( ...

  2. JS中的事件、事件冒泡和事件捕获、事件委托

    https://www.cnblogs.com/diver-blogs/p/5649270.html https://www.cnblogs.com/Chen-XiaoJun/p/6210987.ht ...

  3. 利用python递归实现整数转换为字符串

    def trans(num): if num // 10 == 0: return '%s'%num else: return trans(num//10)+'%s'%(num%10) a=trans ...

  4. 解决wpf popup控件遮挡其他程序的问题

    public class PopupNonTopmost : Popup { public static DependencyProperty TopmostProperty = Window.Top ...

  5. 三. python面向对象

    第七章.面向对象基础 1.面向对象基础 类和对象: a. 创建类 class 类名: def 方法名(self,xxx): pass b. 创建对象 对象 = 类名() c. 通过对象执行方法 对象. ...

  6. ios调试小结

    Xcode底部的小黑盒是我们调试时的好朋友,它可以输出日志信息.错误信息以及其他有用的东西来帮你跟踪错误,除了可以看到日志直接输出的信息外,我们编程过程中也可以在某些断点停留,来检查app的多个方面. ...

  7. x220 OS X 10.10.4安装

    变色龙安装过程: 1.使用磁盘助手将按照盘写入独立的磁盘分区(AF格式,就是Apple的HPS格式): 2.安装启动时,用-v -f -x参数,分别为显示信息.重新build系统驱动.安全模式: 3. ...

  8. 一次下载多个文件的解决思路-JS

    一次下载多个文件的解决思路(iframe) - Eric 真实经历 最近开发项目需要做文件下载,想想挺简单的,之前也做过,后台提供下载接口,前端使用window.location.href就行了呗.不 ...

  9. 【mysql】 load local data infield 报错 ERROR 1148 (42000): The used command is not allowed with this MySQL version

    mysql> load data local infile '/Users/flint/learn/mysql/pet' into table bx_pet; 执行报错 ERROR 1148 ( ...

  10. Python面向对象(成员)(二)

    1. 成员 在类中你能写的所有内容都是类的成员 2. 变量 1. 实例变量: 由对象去访问的变量. class Person: def __init__(self, name, id, gender, ...