网页中css, js, img文件下载保存

# -*- coding:utf-8 -*-
from bs4 import BeautifulSoup as BS 
import urllib.request as rqst

html = rqst.urlopen(url)

#网页用bs解析
bs = BS(req, 'lxml')

#获取css,js,img文件的路由
elc = bs.find_all('link', type='text/css')
elj = bs.find_all('script', type='text/javascript')
eli = bs.find_all('img')


#保存css,js,img文件
for c in elc:
   #还没完成
   url = c['href']
   name = url.split('/')[-1]

for i in eli:
   req = getRequest(i)
   res = rqst.urlopen(req)
   img = res.read()

   name = i.split('/')[-1]

   with open(name, 'wb') as f:
      f.write(img)
   f.close()  

猜你喜欢

转载自www.cnblogs.com/hanzg/p/10941838.html