今天有朋友叫我解析以下img的url,然后下载图片,这里我分享一下我的下载过程。
import urllib.request
from bs4 import BeautifulSoup
#获取页面信息
def getHtml(url):
html = urllib.request.urlopen(url).read()
return html
url='https://partner.googleadservices.com/gampad/ads?gct=-hB1lC5stH0KcooYbypjCAIQFEoAWICAgKDn4PjoxwG4Aafg8JuDBNICCWltYWdlL2dpZvACrAL4AvoBkAMAyAOsAtAD-gHwAwLKBSJodHRwczovL3d3dy5hcm1hemVtcGIuY29tLmJyLz9HRVBC6gYAsgECGAHKAQJAARiLy4ryBSgAMIvnv_kFOABYAWoGX2JsYW5rcP_ehfIF&iu=95377733&gdfp_req=1&height=250&width=300&impl=ifr'
html=getHtml(url)
bs = BeautifulSoup(html,"html.parser") # 缩进格式
for item in bs.find_all("img"):
print(item)
img_path=item.get('src')
if(img_path.startswith('https://')):
urllib.request.urlretrieve(img_path, 'img.jpg')
这是演示代码,读者可以根据需要进行修改。
参考文献
[1].Python下载URL图片. https://www.jianshu.com/p/546a0c1d89e7
[2].python 下载图片.https://blog.youkuaiyun.com/qq_18525247/article/details/80812823