2013-01-10 77 views
0

我想开发python脚本来从domaintools.com托管公司信息,下面是我的脚本。 somthing这个认证部分错误,它返回403错误。用户身份验证whois.domaintools.com python脚本

domain_tools_url = 'https://secure.domaintools.com/log-in/' 
username = '[email protected]' 
password = 'password' 
sys.path.append("./BeautifulSoup") 

passman = urllib2.HTTPPasswordMgrWithDefaultRealm() 
passman.add_password(None, domain_tools_url, username, password) 
authhandler = urllib2.HTTPBasicAuthHandler(passman) 
opener=urllib2.build_opener(authhandler, urllib2.HTTPHandler(debuglevel=0)) 
opener.addheaders = [('User-agent', 'Mozilla/5.0')] 
url = "http://whois.domaintools.com/62.75.xxx.xxx" 
page = opener.open(url) 

可我知道如何解决这个问题,

感谢提前:)

+0

['requests'(http://docs.python-requests.org/en/latest/)使这个要好很多 - 可能是值得一试。基本身份验证只是'requests.get('http://example.com',auth =('user','pass'))' –

+0

嗨@AlexL,感谢您的回放。我已经安装了请求lib,并且我已经运行了脚本,它返回[Response 200] In [6]:requests.get(domain_tools_url,auth =(username,password)) Out [6]: then我如何处理这个URL =“http://whois.domaintools.com/62.75.xxx.xxx” – AGR

+0

认证后,它返回403响应'在[9]:requests.get(“http://whois.domaintools。 com/62.75.xxx.xxx“,auth =(username,password)) Out [9]: In [10]:requests.post(”http://whois.domaintools.com /62.75.xxx.xxx“) 输出[10]:<响应[403]>' – AGR

回答

0

那我该怎么处理这个URL =“whois.domaintools.com/62.75.xxx。 XXX”

相反解析HTML的,我建议使用domaintools自己的API,让你在一条笔直的路需要在不走弯路的数据(第三方库)

http://www.domaintools.com/api/

DomainTools如果你需要更多的提供的WHOIS 500个查询/月的免费和订阅。

import urllib.request 
import json 

# please take notice that this is only a sample query 
# you usually need to authenticate your request: http://www.domaintools.com/api/docs/authentication/ 
data = json.loads(urllib.request.urlopen('http://freeapi.domaintools.com/v1/domaintools.com/whois/').read().decode('utf-8')) 

def readValues(obj): 
    if isinstance(obj, str): 
     print(obj) 
    elif isinstance(obj, dict): 
     for value in obj.values(): 
      readValues(value) 
    elif isinstance(obj, list): 
     for item in obj: 
      readValues(item) 

readValues(data) 

它在Python 3,仅供参考