通过ip获取并下载网页html,出现错误
import sys, socketimport urllib.request
try:
str = input("input:")
result = socket.gethostbyaddr(str)
print ("Primary hostname:")
print (" " + result[0])
# Display the list of available addresses
#that is also returned
print ("\nAddresses:")
for item in result[2]:
print (" " + item)
except socket.herror as e:
print ("Couldn't look up name:", e)
def getHtml(url):
html = urllib.request.urlopen(url).read()
return html
def saveHtml(file_name, file_content):
# 注意windows文件命名的禁用符,比如 /
with open(file_name.replace('/', '_') + ".html", "wb") as f:
# 写文件用bytes而不是str,所以要转码
f.write(file_content)
html = getHtml('http:'+ result[0])
saveHtml("text1", html)
print("结束")
报错 : urllib.error.URLError: <urlopen error no host given>