将网页保存为计算机上的html文件

时间:2015-03-05 02:57:44

标签: html http web httprequest diff

我想将网站页面的源代码保存到我的计算机中。我知道我必须使用http请求将我的网页源代码作为html文件下载到计算机中。我想运行差异来跟踪网页的两个html文件之间的变化。我想知道如何实现一个程序来执行将网页保存为我的计算机上的html文件的功能。请帮助它真的很感激。我想以编程方式解决问题。我正在研究这个主题,发现httpget和selenium脚本可以完成这个任务,但我正在努力实现。

1 个答案:

答案 0 :(得分:0)

使用linux,你可以使用wget。

wget http://google.com

将在您的计算机上保存名为index.html的文件。

以编程方式,您可以使用python:

import urllib2

# create a list of urls that you want to download
urls = ['http://example.com', 'http://google.com']

# loop over the urls
for url in urls:
    # make the request
    request = urllib2.urlopen(url)
    # make the filename valid (you can change this to suit your needs)
    filename = url.replace('http://', '')
    filename = filename.replace('/','-')
    # write it to a file.
    with open(filename + '.html', 'w') as f:
        f.write(request.read())