python - 如何保存网页的源代码
问题描述
您好,我想将网页的源代码保存到任何文本文件并将其保存在 C:\ 文件夹中。
我尝试使用以下代码获取网页的源代码:
html = driver.page_source
print(html)
如何将页面源代码保存到文本文件中的 C:\ 文件夹?
解决方案
尝试这个:
import urllib.request
site = urllib.request.urlopen('http://somesite.com')
data = site.read()
file = open("C:\\file.txt","wb") #open file in binary mode
file.writelines(data)
file.close()
或这个
import urllib.request
def extractHTML(url):
urllib.request.urlretrieve(url, 'C:\\file.txt')
在此处查看更多详细信息
推荐阅读
- azure-devops - 如何在 Azure DevOps Boards 中按 build 或 release 查询工作项?
- android - 防止线性布局在水平方向从其侧面推动元素
- javascript - 使用 libheif 将 Heic 转换为 jpg
- html - 将标题放在上方并居中
- reactjs - React hooks 依赖,包括它会创建一个无限循环,不包括它不会给我最新的价值
- python - 抓取警报管理器 API
- javascript - 将数据从模板传递到 django 的 views.py
- azure - Analysis Services 查询副本:仅在主副本上加载和处理单个数据模型而不从副本中删除?
- python - 遗传算法如何在不知道搜索量的情况下优化神经网络的权重?
- javascript - Javascript 暂时中断链接点击的正常行为