用python将某个网站的HTML保存在一个txt文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24297257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Save HTML of some website in a txt file with python
提问by AndresDuque
I need save the HTML code of any website in a txt file, is a very easy exercise but I have doubts with this because a have a function that do this:
我需要将任何网站的 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()
But this doesn't work.
但这不起作用。
采纳答案by elyase
Easiest way would be to use urlretrieve:
最简单的方法是使用urlretrieve:
import urllib
urllib.urlretrieve("http://www.example.com/test.html", "test.txt")
For Python 3.x the code is as follows:
对于 Python 3.x,代码如下:
import urllib.request
urllib.request.urlretrieve("http://www.example.com/test.html", "test.txt")
回答by Serhii
I use Python 3
.pip install requests
- after install requests
library you can save a webpage in txt file.
我用Python 3
. pip install requests
- 安装requests
库后,您可以将网页保存在 txt 文件中。
import requests
url = "https://stackoverflow.com/questions/24297257/save-html-of-some-website-in-a-txt-file-with-python"
r = requests.get(url)
with open('file.txt', 'w') as file:
file.write(r.text)