大理音乐节,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()
最终成果如下:
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!!
点击进行留言回复
相关文章:
-
-
python如何查看网页代码
用python查看网页代码的方法:1、使用“import”导入requests包import requests2、使用requests包的get()函数通过网页...
[阅读全文]
-
-
python如何保存文本文件
python保存文本文件的方法:使用python内置的open()类可以打开文本文件,向文件里面写入数据可以用write()函数,写完之后,使用close()函...
[阅读全文]
-
python如何编写win程序
python可以编写win程序。win程序的格式是exe,下面我们就来看一下使用python编写exe程序的方法。编写好python程序后py2exe模块即可将...
[阅读全文]
-
-
-
-
-
-
网友评论