使用python检查多个服务中的现有帐户

发布于 2024-11-16 11:52:38 字数 2021 浏览 2 评论 0原文

我正在尝试使用 urllib 和 urlib2 来检查各种社交网络中是否存在公共用户配置文件。 现在我被困在检查 www.live.com 上。 例如,如果我访问此网址 http://spaces.live.com/[email protected],并且mem参数的电子邮件存在,它重定向到此配置文件帐户,例如 http://profile.live.com/cid-f5ee5e2a441e7771/,即使个人资料不公开。否则该帐户不存在。

我应该如何使用 URLError (或其他)来检测重定向?有没有更好的办法呢?

编辑:

自我解决!

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import urllib2 
from urllib2 import HTTPError, URLError
nick=str(sys.argv[1])
pref_live="http://spaces.live.com/profile.aspx?mem="
suf_live_01="@hotmail.com"
try:  
    f = urllib2.urlopen( pref_live + nick + suf_live_01 )
    print f.read()  
    f.close()  
except HTTPError, e:  
    print "error"  
    print e.code  
except URLError, e:  
    print "error"  
    print e.reason  

如果错误是 404,则帐户存在,否则 (500),则不存在

编辑 2:

这是最终代码,谢谢大家的帮助:)

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import urllib2 
from urllib2 import HTTPError, URLError
prefix_live="http://spaces.live.com/profile.aspx?mem="
sufix_live=["@hotmail.com","@live.com"]
try:
    nick=str(sys.argv[1])
except:
    print "Username needed"
    print "Usage:"
    print sys.argv[0], "[username]"
    nick=''

def checking():
    for domain in sufix_live:
        try:  
            f = urllib2.urlopen( prefix_live + nick + domain )
            print f.read()    
            f.close()    
        except HTTPError, e:  
            if e.code == 404:
                print 'Yeah! %s%s exists' % (nick, domain) 
            elif e.code == 500:
                print 'Doh! %s%s Does NOT exists'% (nick, domain)
            else:
                print 'other error'
                print e.code
        except URLError, e:     
            print "There was an error"  
            print e.reason    

if nick != '':
    checking()

I'm trying to use urllib and urlib2 to check if a public user profile exists in various socialnets.
Now i'm stuck trying to checking www.live.com.
If i visit for example this url http://spaces.live.com/[email protected], and the email of mem param exists, it redirects to the profile of this account, like this http://profile.live.com/cid-f5ee5e2a441e7771/ even if the profile is not public. Otherwise the account does not exist.

How should i use URLError (or else) to detect the redirect? Is there any better way to do it?

EDIT:

Self Solved!!!

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import urllib2 
from urllib2 import HTTPError, URLError
nick=str(sys.argv[1])
pref_live="http://spaces.live.com/profile.aspx?mem="
suf_live_01="@hotmail.com"
try:  
    f = urllib2.urlopen( pref_live + nick + suf_live_01 )
    print f.read()  
    f.close()  
except HTTPError, e:  
    print "error"  
    print e.code  
except URLError, e:  
    print "error"  
    print e.reason  

If error is 404, account exists, else (500), it does not exists

EDIT 2:

here is the final code, thank you guys for your help :)

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import urllib2 
from urllib2 import HTTPError, URLError
prefix_live="http://spaces.live.com/profile.aspx?mem="
sufix_live=["@hotmail.com","@live.com"]
try:
    nick=str(sys.argv[1])
except:
    print "Username needed"
    print "Usage:"
    print sys.argv[0], "[username]"
    nick=''

def checking():
    for domain in sufix_live:
        try:  
            f = urllib2.urlopen( prefix_live + nick + domain )
            print f.read()    
            f.close()    
        except HTTPError, e:  
            if e.code == 404:
                print 'Yeah! %s%s exists' % (nick, domain) 
            elif e.code == 500:
                print 'Doh! %s%s Does NOT exists'% (nick, domain)
            else:
                print 'other error'
                print e.code
        except URLError, e:     
            print "There was an error"  
            print e.reason    

if nick != '':
    checking()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸢与 2024-11-23 11:52:38

我会添加一条评论,要求澄清和一个代码示例,但可惜我还不能添加评论。不过,我会在黑暗中疯狂地回答你的问题。

如果我们假设您正在做这样的事情:

request = urllib2.Request('http://someurl.com')
response = urllib2.urlopen(request)
if response.geturl() != 'http://someurl.com':
    print "redirected"
else:
    print "not redirected"   

那么在第一个实例中,如果 urllib2 获取重定向代码(例如响应 300),它将为您处理重定向并获取服务器重定向到的 URL。

然后,您可以通过检查 response.geturl() 的值来确定您是否确实被重定向

I would add a comment asking for clarification and a code sample, but alas I can't add comments yet. However I'll take a wild stab in the dark at answering your question.

If we assume you are doing something like this:

request = urllib2.Request('http://someurl.com')
response = urllib2.urlopen(request)
if response.geturl() != 'http://someurl.com':
    print "redirected"
else:
    print "not redirected"   

then in the first instance if urllib2 gets a redirect code (e.g. response 300) it will handle the redirection for you and fetch the URL that the server redirects to.

You can then establish whether in fact you were redirected by checking the value of response.geturl()

终止放荡 2024-11-23 11:52:38

考虑使用 mechanize 模块。

它导出了 urllib2 的接口,并且还包含许多用于浏览网站的方便的东西(内容解析、表单、重定向、cookie 等)。

consider using the mechanize module.

It exports the interface of urllib2, and also includes a lot of handy stuff for navigating web sites (content parsing, forms, redirects, cookies, etc).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文