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

 

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

from urllib.request import urlretrieve
urlretrieve('http://www.python.org',r'D:\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)

 

posted @ 2018-01-31 21:40  SogetsuKazama  阅读(610)  评论(0)    收藏  举报