文中案例为大伙儿共享了python抓取网页页面中连接的静态图片的实际编码,供大伙儿参照,主要内容以下

# -*- coding:utf-8 -*- 
#http://tieba.baidu.com/p/2460150866 
#抓取图片地址 
from bs4 import BeautifulSoup 
import urllib.request 
from time import sleep 
html_doc = "http://tieba.baidu.com/p/2460150866" 
def get_image(url): 
 req = urllib.request.Request(url) 
 webpage = urllib.request.urlopen(req) 
 html = webpage.read() 
 soup = BeautifulSoup(html, 'html.parser') 
 #抓取图片地址 
 #抓取img标签且class为BDE_Image的全部內容 
 img_src=soup.findAll("img",{'class':'BDE_Image'}) 
 i = 1 
 for img in img_src: 
  img_url = img.get('src') #抓取src 
 # print(img) 
  req = urllib.request.Request(img_url) 
  u = urllib.request.urlopen(req) 
  data = u.read() 
  with open("AutoCodePng20180119-" str(i) ".jpg", 'wb') as f: 
   sleep(2) 
   f.write(data) 
   i  = 1 
def getImg(url): 
 html = urllib.request(url) 
 page = html.read() 
 soup = BeautifulSoup(page, "html.parser") 
 imglist = soup.find_all('img') #发觉html中带img标签的数据信息,輸出格式为<img xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,存进结合 
 lenth = len(imglist) #测算结合的数量 
 for i in range(lenth): 
  print imglist[i].attrs['src'] #抓取img中特性为src的信息内容,比如<img src="123456" xxxxxxxxxxxxxxxx,则輸出为123456 

之上便是文中的所有内容,期待对大伙儿的学习培训有一定的协助,也期待大伙儿多多的适用。