如何将网站的源代码复制到 Python 3 中的文本文件中?
编辑:
为了澄清我的问题,这是我所拥有的:
import urllib.request
def extractHTML(url):
f = open('temphtml.txt', 'w')
page = urllib.request.urlopen(url)
pagetext = page.read()
f.write(pagetext)
f.close()
extractHTML('http:www.google.com')
我收到 f.write() 函数的以下错误:
builtins.TypeError: must be str, not bytes
最佳答案
import urllib.request
site = urllib.request.urlopen('http://somesite.com')
data = site.read()
file = open("file.txt","wb") #open file in binary mode
file.writelines(data)
file.close()
未经测试,但应该工作。
编辑:为python3更新
关于python-3.x - 将 HTML 源代码保存到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9968091/