Python:
下载网页
How to: (如何操作)
使用Python的requests库能轻松完成网页下载。下面是个示例:
import requests
url = 'https://www.example.com'
response = requests.get(url)
# 确认请求成功
if response.status_code == 200:
content = response.text
print(content[:100]) # 打印前100个字符作为示例
else:
print('Failed to retrieve the web page.')运行以上代码,你可能会得到类似这样的输出:
<!doctype html>
<html>
<head>
<title>Example Domain</title>
...Deep Dive (深入探究)
早期,网页下载经常使用urllib。现在更多使用requests,它界面友好,易用性强。除了requests,aiohttp为异步操作提供支持。要处理动态内容则需要Selenium或Pyppeteer。
下载网页时需要处理各种细节:如编码问题、网络错误、重定向处理等。requests库隐藏了这些复杂性。
网速慢或网页大时,应该考虑流式下载。requests可以这么做:
response = requests.get(url, stream=True)此外,还需考虑慎用爬虫,遵守robots.txt规则。
See Also (查看更多)
requests库文档: https://docs.python-requests.org/en/latest/- Python官方
urllib文档: https://docs.python.org/3/library/urllib.html Selenium文档: https://selenium-python.readthedocs.io/PyppeteerGitHub页面: https://github.com/pyppeteer/pyppeteer- 关于
robots.txt的信息: https://www.robotstxt.org/