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 天, ...
随机推荐
- 介绍hadoop的好文章
http://www.centoscn.com/image-text/install/2014/1121/4158.html http://www.cnblogs.com/xia520pi/categ ...
- MySQL select * 和把所有的字段都列出来,哪个效率更高?
MySQL select * 和把所有的字段都列出来,哪个效率更高 答案是:如何,都不推荐使用 SELECT * FROM (1)SELECT *,需要数据库先 Query Table Metadat ...
- sysbench0.5安装介绍
sysbench是一个模块化的.跨平台.多线程基准测试工具,主要用于评估测试各种不同系统参数下的数据库负载情况,sysbench支持MySQL.PostgreSQL.Oracle数据库OLTP测试.它 ...
- SWTError: No more handles [gtk_init_check() failed] running platform tests (on Linux)
http://www.lemmster.de/2013-12-19-swterror-no-more-handles-gtk_init_check-failed-running-platform-te ...
- HDU 3586 Information Disturbing (树形DP,二分)
题意: 给定一个敌人的通信系统,是一棵树形,每个节点是一个敌人士兵,根节点是commander,叶子是前线,我们的目的是使得敌人的前线无法将消息传到commander,需要切断一些边,切断每条边需要一 ...
- HDU 4044 GeoDefense (树形DP,混合经典)
题意: 给一棵n个节点的树,点1为敌方基地,叶子结点都为我方阵地.我们可以在每个结点安放炸弹,每点至多放一个,每个结点有ki种炸弹可选,且每种炸弹有一个花费和一个攻击力(1点攻击力使敌人掉1点hp). ...
- Oracle错误(包括PL/SQL)集合与修复
+-----------------------------------------------------------------------+ | 在本篇随笔中,仅根据个人经验累积错误进行描述 ...
- iOS(iPhone,iPad))开发(Objective-C)开发库常用库索引
http://www.code4app.com 这网站不错,收集各种 iOS App 开发可以用到的代码示例 http://www.cocoacontrols.com/ 英文版本的lib收集 ht ...
- fgetc, fgets, getc, getchar, gets, ungetc - 输入字符和字符串
总览 (SYNOPSIS) #include <stdio.h> int fgetc(FILE *stream); char *fgets(char *s, int size, FILE ...
- Bootstrap历练实例:大的按钮
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...