使用 urllib 打开匹配和保存远程文件

Posted 风间苍月

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用 urllib 打开匹配和保存远程文件相关的知识,希望对你有一定的参考价值。

 

利用 urlretrieve 获取远程文件保存到本地

from urllib.request import urlretrieve
urlretrieve(http://www.python.org,rD:\python\python37\python_wegpage.html)

利用 urlopen 和 re 匹配获取远程文件中的数据

from urllib.request import urlopen
import re
wegpage = urlopen(http://www.python.org)
text = wegpage.read()
text = text.decode(utf-8)
m = re.search(<a href="([^"]+)" .*?>about</a>,text,re.IGNORECASE)
m.group(1)

 

以上是关于使用 urllib 打开匹配和保存远程文件的主要内容,如果未能解决你的问题,请参考以下文章