Python抓取Discuz!使用者名稱指令碼代碼_python

來源:互聯網
上載者:User

最近學習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()

最終成果如下:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.