<Date: 2011-11-18>
<Author: medcl>
<Category: Python, 乱搞>
分享个刚写的python脚本,用来抓取新浪微博的数据,其实这个脚本写了很久了,之前用这个脚本抓了几十G的数据,扔硬盘里一直没有用,前阵子win8刚出来,头脑一热就装了,还全格式化了那块盘,后面想要用的时候,才发现忘记把里面抓到数据拷出来了,悲催啊,还好脚本还在,今天完善了下,共享给大家,主要有如下功能:
支持多线程下载
用户id通过文件加载
按时间分目录
通过用户id取模划分子目录(如果你的id够多的话,避免单个文件夹下太多文件)
支持参数配置skip_count
支持下载完之后自动关机
支持命令行参数配置,具体查看帮助: -h
打包下载
阅读这篇文章的其余部分 »

本文来自: 新浪微博数据集抓取脚本
<Date: 2011-04-09>
<Author: medcl>
<Category: Python>
from __future__ import generators # needed for Python 2.2
import sys
import os
def walktree(top = ".", depthfirst = True):
"""Walk the directory tree, starting from top. Credit to Noah Spurrier and Doug Fort."""
import os, stat, types
names = os.listdir(top)
if not depthfirst:
yield top, names
for name in names:
try:
st = os.lstat(os.path.join(top, name))
except os.error:
continue
if stat.S_ISDIR(st.st_mode):
for (newtop, children) in walktree (os.path.join(top, name), depthfirst):
yield newtop, children
if depthfirst:
yield top, names
def makeHTMLtable(top, depthfirst=False):
from xml.sax.saxutils import escape # To quote out things like &
ret = ['<table class="fileList">\n']
mark=0
for top, names in walktree(top):
#ret.append(' <tr><td class="directory">%s</td></tr>\n'%escape(top))
for name in names:
try:
ext=os.path.basename(name).split('.', 1)[1]
if ext=='html' or ext=='htm':
ret.append(' <tr><td class="file"><a href="%s/%s">%s%s</a></td></tr>\n'%(escape(top),escape(name),escape(top),escape(name)))
mark=1
except IndexError:
pass
ret.append('</table>')
if mark ==1:
return ''.join(ret) # Much faster than += method
else:
return ''
def makeHTMLpage(top, depthfirst=False):
return '\n'.join(['< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"',
'"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',
'<html>'
'<head>',
' <title>Search results</title>',
' <style type="text/css">',
' table.fileList { text-align: left; }',
' td.directory { font-weight: bold; }',
' td.file { padding-left: 4em; }',
' </style>',
'</head>',
'<body>',
'<h1>Search Results</h1>',
makeHTMLtable(top, depthfirst),
'</body>',
'</html>'])
if __name__ == '__main__':
if len(sys.argv) == 2:
top = sys.argv[1]
else: top = '.'
print makeHTMLpage(top)
使用方法:walk.py>index.html
另外一种,使用pyh来生成(比较慢).
阅读这篇文章的其余部分 »

本文来自: python,遍历目录生成html文件列表
<Date: 2011-04-07>
<Author: medcl>
<Category: Python>
url= 'http://10.129.8.58:9200/trendingtopics/%s' % filename
print post_data
req=urllib2.Request(url)
req.get_method = lambda: 'DELETE'
urllib2.urlopen(req)
url= 'http://10.129.8.58:9200/_bulk'
req=urllib2.Request(url,post_data)
req.get_method = lambda: 'PUT'
urllib2.urlopen(req)
http://apps.hi.baidu.com/share/detail/23498106

本文来自: urllib2的delete操作
<Date: 2011-04-06>
<Author: medcl>
<Category: Python, 小道消息>
make string(ex. '3', '32') left padded with zeroes (ex. '003', '032')
HOWTO:
string1 = '32'
int2 = "%03d" % (int(string1))
print int2

本文来自: python string padding left
<Date: 2011-03-31>
<Author: medcl>
<Category: Python>
匹配中文时,正则表达式规则和目标字串的编码格式必须相同
print sys.getdefaultencoding()
text =u"#who#helloworld#a中文x#"
print isinstance(text,unicode)
print text
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 18: ordinal not in range(128)
print text报错
解释:控制台信息输出窗口是按照ascii编码输出的(英文系统的默认编码是ascii),而上面代码中的字符串是Unicode编码的,所以输出时产生了错误。
改成 print(word.encode('utf8'))即可
阅读这篇文章的其余部分 »

本文来自: python正则的中文处理
<Date: 2010-03-31>
<Author: medcl>
<Category: Python>
python里的range与xrange,都可以进行用来进行迭代处理,但是两者却是不一样的,区别如下:
for x in range(100):会返回1个包含了100个整数的list对象,并进行迭代分别返回给x
for x in xrange(100):也会返回100个整数,不过没有一次返回一个集合,而是依次返回一个变量,然后赋值给x,所以也只占一个整数变量所需的内存的就行了,相对来说,由于每次都要分配内存,性能也有些影响,所以怎么使用要看情况哦。
总结,range创建代价大,消耗内存多,xrange占用内存小,每次请求的效率低一些,有点像.NET里的String和StringBuilder了

本文来自: range与xrange的区别