代码如下:
复制代码 代码如下:
# -*- coding: utf-8 -*-
# author: 天一
# blog: http://www.90blog.org
# version: 1.0
# 功能: python抓取百度站长平台用户名脚本
import urllib
import urllib2
import re
import time
def biduspider():
pattern = re.compile(r'(.*)的个人资料 百度站长社区 ')
uid=1
thedatas = []
while uid theurl = http://bbs.zhanzhang.baidu.com/home.php?mod=space&uid=+str(uid)
uid +=1
theresponse = urllib2.urlopen(theurl)
thepage = theresponse.read()
#正则匹配用户名
thefindall = re.findall(pattern,thepage)
#等待0.5秒,以防频繁访问被禁止
time.sleep(0.5)
if thefindall :
#中文编码防止乱码输出
thedatas = thefindall[0].decode('utf-8').encode('gbk')
#写入txt文本文档
f = open('theuid.txt','a')
f.writelines(thedatas+'\n')
f.close()
if __name__ == '__main__':
biduspider()
最终成果如下:
