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

yipeiwu_com5年前Python爬虫

最近学习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使用tornado实现简单爬虫

本文实例为大家分享了python使用tornado实现简单爬虫的具体代码,供大家参考,具体内容如下 代码在官方文档的示例代码中有,但是作为一个tornado新手来说阅读起来还是有点困难的...

python scrapy爬虫代码及填坑

python scrapy爬虫代码及填坑

涉及到详情页爬取 目录结构: kaoshi_bqg.py import scrapy from scrapy.spiders import Rule from scrapy.lin...

Python编写百度贴吧的简单爬虫

操作:输入带分页的地址,去掉最后面的数字,设置一下起始页数和终点页数 功能:下载对应页码的所有页面并储存为HTML文件,以当前时间命名 代码: # -*- coding: utf-8...

python实现从web抓取文档的方法

本文实例讲述了Python实现从Web的一个URL中抓取文档的方法,分享给大家供大家参考。具体方法分析如下: 实例代码如下: import urllib doc = urllib....

python爬虫 模拟登录人人网过程解析

requests 提供了一个叫做session类,来实现客户端和服务端的会话保持 使用方法 1.实例化一个session对象 2.让session发送get或者post请求 sess...