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

    glibc glibc和libc都是Linux下的C函数库,libc是Linux下的ANSI C的函数库:glibc是Linux下的GUN C的函数库:GNU C是一种ANSI C的扩展实现.ANSI ...

  2. iOS:让UIView覆盖导航栏

    当我们想做一个弹出式菜单时,想将导航栏也一起盖住不显示的话,可以用如下语句实现: UIView* myView = /* 你自定义的view */; UIWindow* currentWindow = ...

  3. UVA 1664 Conquer a New Region (Kruskal,贪心)

    题意:在一颗树上要求一个到其他结点容量和最大的点,i,j之前的容量定义为i到j的路径上的最小边容量. 一开始想过由小到大的去分割边,但是很难实现,其实换个顺序就很容易做了,类似kruskal的一个贪心 ...

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

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

  5. 牛客网NOIP赛前集训营-提高组(第三场)A 管道维修

    https://www.nowcoder.com/acm/contest/174/A 这个的话  一个位置被清理的时间就是它到空白格子/边界的最短路对吧qww 然后求期望的话 假设它在第i步被清理掉的 ...

  6. luogu P2574 XOR的艺术 (线段树)

    luogu P2574 XOR的艺术 (线段树) 算是比较简单的线段树. 当区间修改时.\(1 xor 1 = 0,0 xor 1 = 1\)所以就是区间元素个数减去以前的\(1\)的个数就是现在\( ...

  7. Linux菜鸟起飞之路【二】Linux基本常识

    一.Unix操作系统基本常识 1.什么是Unix? Unix是一个计算机操作系统,是一个用来协调.管理和控制计算机硬件与软件资源的控制程序. 2.Unix操作系统的特点? 多用户与多任务.多用户表示在 ...

  8. css3如何实现click后页面过渡滚动到顶部

    var getTop = document.getElementById("get-top"); var head = document.getElementById(" ...

  9. PyCharm(一)——PyCharm设置SSH远程调试

    一.环境 系统环境:windows10 64位 软件:PyCharm2017.3 本地Python环境:Python2.7 二.配置 2.1配置远程调试 第一步:运行PyCharm,然后点击设置如下图 ...

  10. errno的定义

    ./include/asm-generic/errno-base.h -->包含errno=~ ./arch/arm/include/asm/errno.h -->包含/include/a ...