py 爬取页面http://m.sohu.com 并存储
- 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 并存储的更多相关文章
- [实战演练]python3使用requests模块爬取页面内容
本文摘要: 1.安装pip 2.安装requests模块 3.安装beautifulsoup4 4.requests模块浅析 + 发送请求 + 传递URL参数 + 响应内容 + 获取网页编码 + 获取 ...
- MinerHtmlThread.java 爬取页面线程
MinerHtmlThread.java 爬取页面线程 package com.iteye.injavawetrust.miner; import org.apache.commons.logging ...
- 【java】使用URL和CookieManager爬取页面的验证码和cookie并保存
使用java的net包和io包下的几个工具爬取页面的验证码图片并保存到本地. 然后可以把获取的cookie保存下来,做进一步处理.比如通过识别验证码,进一步使用验证码和用户名,密码,保存下来的cook ...
- scrapy中使用selenium来爬取页面
scrapy中使用selenium来爬取页面 from selenium import webdriver from scrapy.http.response.html import HtmlResp ...
- 爬取豆瓣电影TOP 250的电影存储到mongodb中
爬取豆瓣电影TOP 250的电影存储到mongodb中 1.创建项目sp1 PS D:\scrapy> scrapy.exe startproject douban 2.创建一个爬虫 PS D: ...
- py爬取英文文档学习单词
最近开始看一些整本整本的英文典籍,虽然能看个大概,但是作为四级都没过的我来说还是有些吃力,总还有一部分很关键的单词影响我对句子的理解,因为看的是纸质的,所以查询也很不方便,于是想来个突击,我想把程序单 ...
- python 爬虫之requests爬取页面图片的url,并将图片下载到本地
大家好我叫hardy 需求:爬取某个页面,并把该页面的图片下载到本地 思考: img标签一个有多少种类型的src值?四种:1.以http开头的网络链接.2.以“//”开头网络地址.3.以“/”开头绝对 ...
- python爬取豌豆荚中的详细信息并存储到SQL Server中
买了本书<精通Python网络爬虫>,看完了第6章,我感觉我好像可以干点什么:学的不多,其中的笔记我放到了GitHub上:https://github.com/NSGUF/PythonLe ...
- Python 爬取美女图片,分目录多级存储
最近有个需求:下载https://mm.meiji2.com/网站的图片. 所以简单研究了一下爬虫. 在此整理一下结果,一为自己记录,二给后人一些方向. 爬取结果如图: 整体研究周期 2-3 天, ...
随机推荐
- C 函数库 (libc,glibc,uClibc,newlib)
glibc glibc和libc都是Linux下的C函数库,libc是Linux下的ANSI C的函数库:glibc是Linux下的GUN C的函数库:GNU C是一种ANSI C的扩展实现.ANSI ...
- iOS:让UIView覆盖导航栏
当我们想做一个弹出式菜单时,想将导航栏也一起盖住不显示的话,可以用如下语句实现: UIView* myView = /* 你自定义的view */; UIWindow* currentWindow = ...
- UVA 1664 Conquer a New Region (Kruskal,贪心)
题意:在一颗树上要求一个到其他结点容量和最大的点,i,j之前的容量定义为i到j的路径上的最小边容量. 一开始想过由小到大的去分割边,但是很难实现,其实换个顺序就很容易做了,类似kruskal的一个贪心 ...
- JS中的事件、事件冒泡和事件捕获、事件委托
https://www.cnblogs.com/diver-blogs/p/5649270.html https://www.cnblogs.com/Chen-XiaoJun/p/6210987.ht ...
- 牛客网NOIP赛前集训营-提高组(第三场)A 管道维修
https://www.nowcoder.com/acm/contest/174/A 这个的话 一个位置被清理的时间就是它到空白格子/边界的最短路对吧qww 然后求期望的话 假设它在第i步被清理掉的 ...
- luogu P2574 XOR的艺术 (线段树)
luogu P2574 XOR的艺术 (线段树) 算是比较简单的线段树. 当区间修改时.\(1 xor 1 = 0,0 xor 1 = 1\)所以就是区间元素个数减去以前的\(1\)的个数就是现在\( ...
- Linux菜鸟起飞之路【二】Linux基本常识
一.Unix操作系统基本常识 1.什么是Unix? Unix是一个计算机操作系统,是一个用来协调.管理和控制计算机硬件与软件资源的控制程序. 2.Unix操作系统的特点? 多用户与多任务.多用户表示在 ...
- css3如何实现click后页面过渡滚动到顶部
var getTop = document.getElementById("get-top"); var head = document.getElementById(" ...
- PyCharm(一)——PyCharm设置SSH远程调试
一.环境 系统环境:windows10 64位 软件:PyCharm2017.3 本地Python环境:Python2.7 二.配置 2.1配置远程调试 第一步:运行PyCharm,然后点击设置如下图 ...
- errno的定义
./include/asm-generic/errno-base.h -->包含errno=~ ./arch/arm/include/asm/errno.h -->包含/include/a ...