我需要将任何网站的 HTML 代码保存在 txt 文件中,这是一个非常简单的练习,但我对此表示怀疑,因为有一个函数可以执行此操作:
import urllib.request
def get_html(url):
f=open('htmlcode.txt','w')
page=urllib.request.urlopen(url)
pagetext=page.read() ## Save the html and later save in the file
f.write(pagetext)
f.close()
但这行不通。
最简单的方法是使用网址检索 https://docs.python.org/2/library/urllib.html#urllib.urlretrieve:
import urllib
urllib.urlretrieve("http://www.example.com/test.html", "test.txt")
对于Python 3.x,代码如下:
import urllib.request
urllib.request.urlretrieve("http://www.example.com/test.html", "test.txt")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)