Simple spider with wget
一些临时且定制程度很高的抓取工作,使用系统化的爬虫往往没有使用Shell 效率高,下面是一些思路。
事先用脚本分析出下载连接,存放在一个文件中,然后用split 切割成若干个文件,文件数取决于希望的并发下载进程数量。
使用Wget 进行下载:
nohup cat links.txt.aa |awk '{print "wget \""$0"\" --user-agent=\"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6\" -a wget.log -nv "}'| sh & nohup cat links.txt.ab |awk '{print "wget \""$0"\" --user-agent=\"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6\" -a wget.log -nv "}'| sh & nohup cat links.txt.ac |awk '{print "wget \""$0"\" --user-agent=\"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6\" -a wget.log -nv "}'| sh & |
模拟了UA,当然模拟Cookies、reference 都没有问题,稍后补上吧。
根据wget.log 统计下载速度:
日志格式:
2012-02-01 17:23:32 URL:http://a.b.com/d.zip [196986/196986] -> "d.zip" [1] 2012-02-01 17:23:45 URL:http://a.b.com/e.zip [49455/49455] -> "e.zip" [1] |
One-liner 脚本:
cat ~/wget.log |grep "2012-02-02 17:..:.."|awk '{a+= substr($4, 2, index($4, "/")-2)}END{print a}' |
- 相关文章:
mysql python in lion 64
下载并安装64位版本的MySQL Server community
修改~/.bash_profy:
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/ 下载MySQL-Python,http://mysql-python.sourceforge.net/ 解压缩,并用下面的命令安装:sudo ARCHFLAGS='-arch ix86_64' python setup.py build sudo ARCHFLAGS='-arch ix86_64' python setup.py install 测试:$ python >> import MySQLdb查看共享库的格式:
$ file build/temp.macosx-10.7-ix86_64-2.7/_mysql.o build/temp.macosx-10.7-ix86_64-2.7/_mysql.o: Mach-O 64-bit object x86_64- 相关文章:
Sharing
今天安排了一位资深的Linux C/C++ 的同事为大家做了一次分享,主要将程序运行时在系统底层究竟是什么样子,可惜的是我今天没能参加,不知道效果如何。
这里说说我对“培训”的看法,其实我喜欢叫它“分享”,这样可以降低讲授者的压力。每周我们会安排30-60分钟的时间,请一些同事分享最近对某些技术的心得,目的是大家共同提高,共同进步,培养分享的氛围(我曾经做过一次,专门聊分享的重要性)。在经历了一段时间的实践,我们发现一些问题,虽然也有成功案例,但大多分享很难提起大家的兴趣,台上照本宣科,台下昏昏欲睡——WHY?
** 授人以渔 **
分享,要彻底,毫无保留。不要传授结论,要传授方法。比如,我们做一次Case Study,某网络服务挂掉了,我们用1天的时间解决了,然后我们告诉大家,是因为多进程并发时,某个逻辑中产生了死锁,大家要以后要注意。我想这时一半人已经睡了,我如果知道问题是什么,再弱的人Google 一下也知道如何解决了,分享的意义何在?可悲的是,我们大多数的分享都是这类的。其实,大家更希望了解你是如何发现问题的,你思考的过程,他消化后没准还能举一反三,这才是分享的价值所在。
另一方面,分享不一定把某个问题讲清楚,也许给大家留下一些谜题更有价值。比如之前我们曾做过一次二进制文件的布式存储和分析系统的设计分享,那只是个概念草稿,甚至最终也没有实施,过程中我们聊了,如何给这个问题定性,为什么不使用Hadoop,为什么使用一致性哈希算法,架构为什么设计成这样,等等。大家都在认真的听、思考,并提出了很多问题。虽然这次分享没有什么立竿见影的效果,但不久后,我发现大家在各自架构演进中,都有意无意的引入了一些这次分享中的概念,我想那时埋下的种子——发芽了。
最后:分享,收获>付出。
- 相关文章:
公式计算
看到统计系统的代码往往很罗嗦,写了个小函数,可以一定程度上将逻辑集中,将公式抽取到配置文件中。
有兴趣的朋友可以继续阅读有关闭包的内容。
# encoding: utf8 """ Expr module """ import types def calc(formula, *args): """ 使用给定的参数计算公式的值. @formula: 公式,用'#' 作为参数占位符,比如'#+#',表示两个数相加 @*args: 参数列表,参数可以是函数 例子: - 数值参数:calc('#+#', 1, 1),结果为2 - 函数参数:calc('#-#', time.time, time.time),结果为0 """ parts = [] pos = 0 for c in formula: if c is '#': if pos >= len(args): raise Exception('Index out of range. \ The formula need more arguments') part = args[pos] if callable(part): part = part() part = str(float(part)) pos += 1 else: part = c parts.append(part) return eval(''.join(parts)) if __name__ == '__main__': """ Sample unit test """ import time assert 0 == calc('#-#', time.time, time.time) assert 2 == calc('#+#', 1, 1) print 'Cool! All cases passed.' |
- 相关文章:
门后的秘密
感谢三火兄 赠书!

很高兴的发现,其实书中说的某些实践,我们已经在做了,这些是我们这些未受过专业管理训练的人从实际工作中总结出来的。其实书中的道理同样是从实际工作中总结出来的,不过经历的时间跨度更大、更系统、更精辟,这也是看书的好处,拾遗补缺。
信息很奇妙,无论用什么载体,文字、声音、图像,都可能产生误读,与信息本身无关,问题在于接收者,根据不同的接收者应选择适用的方式。而实践是最好的,告诉人一个道理,不如带领他一起去实践,让他自己去感悟,再去协助总结。
- 相关文章:
Python 实现的一些统计函数
Copy from: http://www.goldb.org/corestats.html
其实都很简单、也很常用,求和、计数、最大最小值、平均值、中位数、标准偏差、百分比。
#!/usr/bin/env python
# corestats.py (COREy STATS)
# Copyright (c) 2006-2007, Corey Goldberg (corey@goldb.org)
#
# statistical calculation class
# for processing numeric sequences
#
# license: GNU LGPL
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
import sys
class Stats:
def __init__(self, sequence):
# sequence of numbers we will process
# convert all items to floats for numerical processing
self.sequence = [float(item) for item in sequence]
def sum(self):
if len(self.sequence) < 1:
return None
else:
return sum(self.sequence)
def count(self):
return len(self.sequence)
def min(self):
if len(self.sequence) < 1:
return None
else:
return min(self.sequence)
def max(self):
if len(self.sequence) < 1:
return None
else:
return max(self.sequence)
def avg(self):
if len(self.sequence) < 1:
return None
else:
return sum(self.sequence) / len(self.sequence)
def median(self):
if len(self.sequence) < 1:
return None
else:
self.sequence.sort()
return self.sequence[len(self.sequence) // 2]
def stdev(self):
if len(self.sequence) < 1:
return None
else:
avg = self.avg()
sdsq = sum([(i - avg) ** 2 for i in self.sequence])
stdev = (sdsq / (len(self.sequence) - 1)) ** .5
return stdev
def percentile(self, percentile):
if len(self.sequence) < 1:
value = None
elif (percentile >= 100):
sys.stderr.write('ERROR: percentile must be < 100. you supplied: %s\n'% percentile)
value = None
else:
element_idx = int(len(self.sequence) * (percentile / 100.0))
self.sequence.sort()
value = self.sequence[element_idx]
return value
# Sample script using this class:
# -------------------------------------------
# #!/usr/bin/env python
# import corestats
#
# sequence = [1, 2.5, 7, 13.4, 8.0]
# stats = corestats.Stats(sequence)
# print stats.avg()
# print stats.percentile(90)
# -------------------------------------------



