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爬虫抓站的实用技巧

总结python爬虫抓站的实用技巧

前言 写过的这些脚本有一个共性,都是和web相关的,总要用到获取链接的一些方法,累积不少爬虫抓站的经验,在此总结一下,那么以后做东西也就不用重复劳动了。 1.最基本的抓站 impor...

python实现爬取千万淘宝商品的方法

本文实例讲述了python实现爬取千万淘宝商品的方法。分享给大家供大家参考。具体实现方法如下: import time import leveldb from urllib.pars...

Python3网络爬虫中的requests高级用法详解

Python3网络爬虫中的requests高级用法详解

本节我们再来了解下 Requests 的一些高级用法,如文件上传,代理设置,Cookies 设置等等。 1. 文件上传 我们知道 Reqeuests 可以模拟提交一些数据,假如有的网站需...

Python简单实现网页内容抓取功能示例

本文实例讲述了Python简单实现网页内容抓取功能。分享给大家供大家参考,具体如下: 使用模块: import urllib2 import urllib 普通抓取实例:...

实例讲解Python爬取网页数据

一、利用webbrowser.open()打开一个网站: >>> import webbrowser >>> webbrowser.open('...