我是 Python 的新手。所以在 newboston 的最新 python 教程中,我制作了一个网络爬虫。然而,此 Python 脚本仅适用于 Linux。 Windows 可以替代以下代码吗?
import os
def get_ip_address(url):
command = "host " + url
process = os.popen(command)
results = str(process.read())
marker = results.find('has address') + 12
return results[marker:].splitlines()[0]
print(get_ip_address('google.com'))
我想要它的格式与我想要编写多个模块(即获取 ip、谁是谁等)并将它们放在一起作为一个 Python 程序的格式相同。
我试图在结果中只获取 IP 地址,没有其他内容。
或者,这是我目前在 Windows 上拥有的:
import socket
def get_ips_for_host(url):
try:
ips = socket.gethostbyname_ex(url)
except socket.gaierror:
ips = []
return ips
ips = get_ips_for_host('www.facebook.com')
print(repr(ips))
如何修改它以使其只获取 IP 地址?
Similarly how can I get the nmap scan and the 'who is'?
最佳答案
the_ip = get_ips_for_host('www.facebook.com')[-1][0]
关于python - 这个 python 脚本的 Windows 替代方案,用于 linux 从 URL 获取 IP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33489786/