注册 登录
编程论坛 Python论坛

通过ip获取并下载网页html,出现错误

dtxwz 发布于 2017-10-25 21:57, 1536 次点击
import sys, socket
import 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>
1 回复
#2
yuccn2017-11-04 09:41
no host given

很清楚了
1