![图片[1]-python如何下载网页?-uusu优素-乐高,模型,3d打印,编程](http://uusucn.zbbe.cn/wp-content/uploads/2024/01/5ec77b38cb67a544.jpg)
python下载网页的方法:
直接使用下面的代码即可下载一个网页:
importurllib.request
defgetHtml(url):
html=urllib.request.urlopen(url).read()
returnhtml
defsaveHtml(file_name,file_content):
#注意windows文件命名的禁用符,比如/
withopen(file_name.replace('/','_')+".html","wb")asf:
#写文件用bytes而不是str,所以要转码
f.write(file_content)
aurl="https://www.py.cn/faq/python/18220.html"
html=getHtml(aurl)
saveHtml("sduview",html)
print("下载成功")
下载文件如下:

程序主要有两个函数,第一个getHtml(url) 功能为打开一个url网络链接,把链接的内容读取出来,存在html变量里;第二个saveHtml(file_name,file_content) 功能为打开一个名为 file_name 的文件,把网页内容写进去。
原文来自:https://www.py.cn© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END


















































暂无评论内容