当前位置: 移动技术网 > IT编程>脚本编程>Python > Python抓取Discuz!用户名脚本代码

Python抓取Discuz!用户名脚本代码

2019年06月14日  | 移动技术网IT编程  | 我要评论

大理音乐节,7日酒店,御筑轩

最近学习python,于是就用python写了一个抓取discuz!用户名的脚本,代码很少但是很搓。思路很简单,就是正则匹配title然后提取用户名写入文本文档。程序以百度站长社区为例(一共有40多万用户),挂在vps上就没管了,虽然用了延时但是后来发现一共只抓取了50000多个用户名就被封了。。。
代码如下:

复制代码 代码如下:

# -*- 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'<title>(.*)的个人资料  百度站长社区 </title>')
     uid=1
     thedatas = []
     while uid <400000:
         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()

最终成果如下:

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网