在字符串中查找子字符串后检索下一个x字符

时间:2015-11-08 11:13:06

标签: python string python-3.x

我对python很新。 我试图通过脚本从某个域获取whois状态。 我使用的是Microsoft的whois(https://technet.microsoft.com/en-us/sysinternals/whois)。

我需要运行一个简单的命令来检索状态

whois stackoverflow.com

有了这个,我会得到一个如下所示的输出:

L:\Programming>hello.py
Starting...

Whois v1.12 - Domain information lookup utility
Sysinternals - www.sysinternals.com
Copyright (C) 2005-2014 Mark Russinovich

Connecting to COM.whois-servers.net...
Connecting to whois.name.com...

Domain ID: 108907621_DOMAIN_COM-VRSN
Registrar WHOIS Server: whois.name.com
Registrar URL: http://www.name.com
Updated Date: 2014-05-09T17:51:17-06:00Z
Creation Date: 2003-12-26T19:18:07-07:00Z
Registrar Registration Expiration Date: 2015-12-26T19:18:07-07:00Z
Registrar: Name.com, Inc.
Registrar IANA ID: 625
Reseller:
Domain Status: clientTransferProhibited
Registry Registrant ID:
Registrant Name: Sysadmin Team
Registrant Organization: Stack Exchange, Inc.
Registrant Street: 1 Exchange Plaza , Floor 26
Registrant City: New York
Registrant State/Province: NY
Registrant Postal Code: 10006
Registrant Country: US
Registrant Phone: +1.2122328280
Registrant Email: sysadmin-team@stackoverflow.com
Registry Admin ID:
Admin Name: Sysadmin Team
Admin Organization: Stack Exchange, Inc.
Admin Street: 1 Exchange Plaza , Floor 26
Admin City: New York
Admin State/Province: NY
Admin Postal Code: 10006
Admin Country: US
Admin Phone: +1.2122328280
Admin Email: sysadmin-team@stackoverflow.com
Registry Tech ID:
Tech Name: Sysadmin Team
Tech Organization: Stack Exchange, Inc.
Tech Street: 1 Exchange Plaza , Floor 26
Tech City: New York
Tech State/Province: NY
Tech Postal Code: 10006
Tech Country: US
Tech Phone: +1.2122328280
Tech Email: sysadmin-team@stackoverflow.com
Name Server: cf-dns02.stackoverflow.com
Name Server: cf-dns01.stackoverflow.com
DNSSEC: Unsigned Delegation
Registrar Abuse Contact Email: abuse@name.com
Registrar Abuse Contact Phone: +1.17203101849
URL of the ICANN WHOIS Data Problem Reporting System: http://wdprs.internic.net/

>>> Last update of WHOIS database: 2015-11-08T04:07:29-07:00 <<<


The Data in the Name.com, Inc. WHOIS database is provided by Name.com, Inc. for
information purposes, and to assist persons in obtaining information about or re
lated to a domain name registration record.  Name.com, Inc. does not guarantee i
ts accuracy.  By submitting a WHOIS query, you agree that you will use this Data
 only for lawful purposes and that, under no circumstances will you use this Dat
a to:  (1) allow, enable, or otherwise support the transmission of mass unsolici
ted, commercial advertising or solicitations via e-mail (spam); or (2) enable hi
gh volume, automated, electronic processes that apply to Name.com, Inc. (or its
systems). Name.com, Inc. reserves the right to modify these terms at any time.
By submitting this query, you agree to abide by this policy.




Found!

我想要实现的是从该域中检索过期数据,并将其放入文件中。我可以找到&#34;注册商注册到期日期:&#34;存在,但如何在&#34;日期之后检索日期:&#34; ?我希望你能帮助我。这就是我得到的安慰

print("Starting...")
import subprocess

findStatus = "Registrar Registration Expiration Date:"

whoOutput = subprocess.Popen(["whois", "stackoverflow.com"],shell=True,stdout=subprocess.PIPE).stdout.read().decode("utf-8")

print(whoOutput)

if findStatus in whoOutput:
    print("\nFound!")
else:
    print("\nNot found")

2 个答案:

答案 0 :(得分:1)

使用正则表达式:

import re
re.search('Registrar Registration Expiration Date: (.*)', whoOutput)

没有正则表达式:

[i.replace('Registrar Registration Expiration Date: ', '') for i in whoOutput.splitlines() if 'Registrar Registration Expiration Date' in i][0]

或更简单:

[i.split(': ')[1] for i in whoOutput.splitlines() if 'Registrar Registration Expiration Date' in i][0]

答案 1 :(得分:0)

您想要的是调用whois,遍历结果,使用正则表达式扫描每一行并在找到匹配项时写入文件。像这样:

error: 'lol'
相关问题