【发布时间】:2017-10-09 15:55:31
【问题描述】:
我有一个看起来像这样的函数,它在给定 url 时查找 who.is 上的域:
import whois
def who_is(url):
w = whois.whois(url)
return w.text
返回一个巨大的字符串:
Domain name:
amazon.co.uk
Registrant:
Amazon Europe Holding Technologies SCS
Registrant type:
Unknown
Registrant's address:
65 boulevard G-D. Charlotte
Luxembourg City
Luxembourg
LU-1311
Luxembourg
Data validation:
Nominet was able to match the registrant's name and address against a 3rd party data source on 10-Dec-2012
Registrar:
Amazon.com, Inc. t/a Amazon.com, Inc. [Tag = AMAZON-COM]
URL: http://www.amazon.com
Relevant dates:
Registered on: before Aug-1996
Expiry date: 05-Dec-2020
Last updated: 23-Oct-2013
Registration status:
Registered until expiry date.
Name servers:
ns1.p31.dynect.net
ns2.p31.dynect.net
ns3.p31.dynect.net
ns4.p31.dynect.net
pdns1.ultradns.net
pdns2.ultradns.net
pdns3.ultradns.org
pdns4.ultradns.org
pdns5.ultradns.info
pdns6.ultradns.co.uk 204.74.115.1 2610:00a1:1017:0000:0000:0000:0000:0001
WHOIS lookup made at 21:09:42 10-May-2017
--
This WHOIS information is provided for free by Nominet UK the central registry
for .uk domain names. This information and the .uk WHOIS are:
Copyright Nominet UK 1996 - 2017.
You may not access the .uk WHOIS or use any data from it except as permitted
by the terms of use available in full at http://www.nominet.uk/whoisterms,
which includes restrictions on: (A) use of the data for advertising, or its
repackaging, recompilation, redistribution or reuse (B) obscuring, removing
or hiding any or all of this notice and (C) exceeding query rate or volume
limits. The data is provided on an 'as-is' basis and may lag behind the
register. Access may be withdrawn or restricted at any time.
所以只要看看它,我就可以看到布局可以将它变成字典,但不确定如何以最有效的方式实际执行它。我需要删除底部不需要的文本,并删除所有换行符和缩进。单独完成不是很有效。我希望能够将任何 url 传递给函数并使用字典。任何帮助将非常感激。
期望的输出是:
dict = {
'Domain name':'amazon.co.uk',
'Registrant':'Amazon Europe Holding Technologies'
'Registrant type': 'Unknown'
and so on for all the available fields.
}
到目前为止,我已经尝试使用 remove 函数删除所有 \n 新行和 \r,然后用 replace 函数替换所有缩进。但是我完全不确定如何删除底部的大量文本。
python-whois 文档告诉您只打印 w 但是这样做时它会返回以下内容:
{
"domain_name": null,
"registrar": null,
"registrar_url": "http://www.amazon.com",
"status": null,
"registrant_name": null,
"creation_date": "before Aug-1996",
"expiration_date": "2020-12-05 00:00:00",
"updated_date": "2013-10-23 00:00:00",
"name_servers": null
}
如您所见,其中大部分值是 null,但在返回 w.text 时,它们确实有值
【问题讨论】:
-
@abccd 我已经添加了一些说明,我并没有要求有人为我做这件事,如果遇到这种情况,我很抱歉,我正在寻找更多关于如何去的想法关于有效地做到这一点。我可以通过将字符串转换为列表并使用替换删除它们来删除所有缩进,然后删除所有
\n和\rs,然后将其重新转换为由:拆分的字符串,然后我会让所有偶数索引成为键,奇数索引成为值。但这似乎效率不高,而且似乎是不好的做法。 -
说真的,python-whois 看起来像一个不错的库,任何解析 w.text 的尝试都会真正违背它的目的。为您的用例修复它确实应该是要走的路。不幸的是,它依赖于正则表达式,如果你不熟悉这些可能会很痛苦。但是,如果您打开一张包含所有所需信息(不多,只是 URL 和您的输出)的票证,开发人员可能会为您解决问题...
标签: python string python-3.x dictionary