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

来源:互联网 发布:撕衣服软件下载 编辑:程序博客网 时间:2024/06/10 13:02
# _*_ 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()
0 0
原创粉丝点击