最近學習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()
最終成果如下:
![]()