下载html页面及其内容

2024-04-26 22:41:47 发布

您现在位置:Python中文网/ 问答频道 /正文


Tags: python
3条回答

您可以使用urlib:

import urllib.request

opener = urllib.request.FancyURLopener({})
url = "http://stackoverflow.com/"
f = opener.open(url)
content = f.read()

你要找的是镜像工具。如果您想要Python中的一个,PyPI会列出spider.py,但我没有使用它的经验。其他的可能更好,但我不知道-我使用“wget”,它支持getting the CSS和图像。这可能是你想要的(引用the manual

Retrieve only one HTML page, but make sure that all the elements needed for the page to be displayed, such as inline images and external style sheets, are also downloaded. Also make sure the downloaded page references the downloaded links.

wget -p --convert-links http://www.server.com/dir/page.html

您可以使用^{}模块下载单个url,但这只会返回数据。它不会解析HTML并自动下载CSS文件和图像等内容。

如果你想下载“整个”页面,你需要解析HTML并找到你需要下载的其他东西。您可以使用Beautiful Soup之类的东西来解析您检索的HTML。

This question有一些示例代码正是这样做的。

相关问题 更多 >