您的位置:首页 > 编程语言 > Python开发

可用于获取百度贴吧的帖子中的Email地址的Python脚本

2016-07-28 14:37 477 查看
# _*_ coding:utf-8 _*_

import urllib,urllib2

import re

import time

print('该脚本可用于获取百度贴吧的帖子中的Email地址,获取后保存在D:\Email.txt中,可能需要权限创建这个文件,如可能请以管理员身份运行')

print('网页URL中含有#的有可能失败(#是python的注释标志)')

myUrl=raw_input('请输入网页URL:')

minIndex=int(input('请输入起始页码:'))

maxIndex=int(input('请输入终止页码:'))

firstPattern=re.compile(r'(\?pn=\d+)$')

myUrl=re.sub(firstPattern,'',myUrl)

try:

    fp=open(r'D:\Email.txt','a+')

    print(time.strftime('%Y-%m-%d-%H-%M-%S:',time.localtime(time.time())))

    fp.write(time.strftime('\n%Y-%m-%d-%H-%M-%S:\n',time.localtime(time.time())))

    for i in range(minIndex,maxIndex+1):

        index=myUrl.rfind(r'?pn=')

        if index==-1:

            myUrl=myUrl+r'?pn='+str(i)

        else:

            myUrl=re.sub(firstPattern,r'?pn='+str(i),myUrl)

        print(myUrl)

        #rep=urllib.Request(myUrl)

        rep=urllib2.Request(myUrl)

      #  rep=urllib.urlopen(myUrl)

        response=urllib2.urlopen(rep)

        myPage=response.read()

        myPage=myPage.decode('utf-8')

        myPage=myPage.replace(r'\r\n','')

        pattern=re.compile(r'([a-zA-Z0-9]+@[a-zA-Z0-9]+\.?[a-zA-Z0-9]+\.+[a-zA-Z0-9]+)')

        result=pattern.findall(myPage)

        if result is not None:

            for email in result:

                print(email)

                fp.write(email+';')

        else:

            print("not found")

    fp.close()

    print('Suceed!!!')

except Exception as e:

    print(e.message)

    fp.close()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: