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. APP弱网测试点

  2. vuex存取token,http简单封装、模拟登入权限校验操作、路由懒加载的几种方式、vue单页设置title

    1.config index.js下面的跨域代理设置: proxyTable: { '/api': { target: 'http://xxxx', //要访问的后端接口 changeOrigin: ...

  3. Eclipse Java类编辑器里出现乱码的解决方案

    如图:在Java Class编辑器里出现的这种乱码,非常烦人. 解决方案:Windows->Preference->General->Appearance, 在里面将Theme设置成 ...

  4. 一个简单的例子教会您使用javap

    javap是JDK自带的工具: 这篇文章使用下面这段简单的Java代码作为例子进行讲解. class Outer { Nested nested; Nested getNested() { retur ...

  5. mysql利用binlog恢复数据详细例子

    模拟数据恢复的案例 有些时候脑瓜就会短路,难免会出错 场景:在生产环境中,我们搭建了mysql主从,备份操作都是在从备份数据库上 前提:有最近一天或者最近的全备 或者最近一天相关数据库的备份 最重要的 ...

  6. CPP-基础:TCHAR

    目 录 定义 使用原理 1.定义 TCHAR是通过define定义的字符串宏[1] 2.使用原理 因为C++支持两种字符串,即常规的ANSI编码(使用""包裹)和Unicode编码 ...

  7. iOS开发各种证书问题

    引言 写在前面 一.App ID(bundle identifier)     二.设备(Device)     三.开发证书(Certificates) 四.供应配置文件(Provisioning ...

  8. HDU-2544-最短路(Bellman-Ford)

    Bellman-Ford算法是一个时间复杂度很高,但是它可以用来判断负环 负环就是上面的图,那个环的整体值小于零了,所以就是负环. 我们用Bellman-Ford算法进行更新,打一个表出来: k a ...

  9. dom事件机制系列

    JS事件流机制 一个完整的JS事件流是从window开始,最后回到window的一个过程,事件流被分为三个阶段: (1~5)捕获过程.(5~6)目标过程.(6~10)冒泡过程. 通过addEventL ...

  10. python--管道, 事件, 信号量, 进程池

    一 . 管道 (了解) from multiprocessing import Process, Pipe def f1(conn): # 管道的recv 里面不用写数字 from_main_proc ...