Python crawl discuz! user name scripting code _python

Source: Internet
Author: User

Recently learning Python, so I wrote a script to crawl discuz! username in Python, the code is very small but very rubbing. The idea is simply to match the title and then extract the user name to write to the text document. The program to Baidu Webmaster community as an example (a total of more than 400,000 users), hanging on the VPS on the tube, although the use of the delay but later found that only grabbed more than 50,000 user names were sealed ...
The code is as follows:

Copy Code code as follows:

#-*-Coding:utf-8-*-
# Author: Day One
# blog:http://www.90blog.org
# version:1.0
# function: Python crawl Baidu webmaster Platform user name script

Import Urllib
Import Urllib2
Import re
Import time

Def biduspider ():
Pattern = Re.compile (R ' <title> (. *) 's profile Baidu Webmaster Community </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 Seconds to prevent frequent access being prohibited
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 ()

The final results are as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.