简体   繁体   English

用python将某个网站的HTML保存在一个txt文件中

[英]Save HTML of some website in a txt file with python

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.但这不起作用。

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")

I use Python 3 .我使用Python 3
pip install requests - after install requests library you can save a webpage in txt file. 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM