python-3.x - 将 HTML 源代码保存到文件

标签 python-3.x

如何将网站的源代码复制到 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/

相关文章:

python - 如何用pymongo连接远程mongodb

Python3 UnicodeDecodeError,无法读取 cp1251 编码的文件

python - 从列表中删除方括号 [](同时使用键排序)

python - 属性错误 : module 'alembic.context' has no attribute 'config'

python - 为什么 random.sample 与 multiprocessing.Pool 一起使用有时会死锁?

python - pyCuda,发送多个单变量参数的问题

python - 调试 Python 3 运行时错误

python - Groupby.transform 在 dask 数据帧中不起作用

python - 将子列表作为对象而不是列表复制到主列表中

python - 将 Python 类转换为 Numpy 数组