使用python将某些网站的HTML保存为txt文件 [英] Save HTML of some website in a txt file with python

查看:116
本文介绍了使用python将某些网站的HTML保存为txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将任何网站的HTML代码保存在txt文件中,这是一个非常容易的练习,但是我对此表示怀疑,因为a具有执行此操作的功能:

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:

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

但这是行不通的.

推荐答案

最简单的方法是使用 URL检索:

import urllib

urllib.urlretrieve("http://www.example.com/test.html", "test.txt")

对于Python 3.x,代码如下:

For Python 3.x the code is as follows:

import urllib.request    
urllib.request.urlretrieve("http://www.example.com/test.html", "test.txt")

这篇关于使用python将某些网站的HTML保存为txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆