标题:通过ip获取并下载网页html,出现错误
取消只看楼主
dtxwz
Rank: 2
等 级:论坛游民
帖 子:79
专家分:45
注 册:2011-9-18
结帖率:100%
 问题点数:0 回复次数:0 
通过ip获取并下载网页html,出现错误
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>
搜索更多相关主题的帖子: 网页 html str result print 
2017-10-25 21:57



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-481769-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.164004 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved