存储数据

1、python3+版本中,使用urllib.request.urlretrieve下载文件

#下载一个文件,并且知道如何获取它以及它的文件类型,以下是下载一个文件
from urllib.request import urlretrieve
from urllib.request import urlopen
from bs4 import BeautifulSoup

html = urlopen("http://www.pythonscraping.com")
bs = BeautifulSoup(html)
imagelocation = bs.find("a",{"id":"logo"}).find("img")["src"]
urlretrieve(imagelocation,"logo.jpg")
#以上会在程序运行的文件夹里下载一个名为“logo.jpg”的图片

2

 

posted on 2018-02-02 21:34  告辞  阅读(96)  评论(0)    收藏  举报

导航