python抓取网页中链接的静态图片

yipeiwu_com5年前Python爬虫

本文实例为大家分享了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 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python进阶之使用selenium爬取淘宝商品信息功能示例

本文实例讲述了Python进阶之使用selenium爬取淘宝商品信息功能。分享给大家供大家参考,具体如下: # encoding=utf-8 __author__ = 'Jonny'...

Python编写百度贴吧的简单爬虫

操作:输入带分页的地址,去掉最后面的数字,设置一下起始页数和终点页数 功能:下载对应页码的所有页面并储存为HTML文件,以当前时间命名 代码: # -*- coding: utf-8...

python3第三方爬虫库BeautifulSoup4安装教程

python3第三方爬虫库BeautifulSoup4安装教程

Python3安装第三方爬虫库BeautifulSoup4,供大家参考,具体内容如下 在做Python3爬虫练习时,从网上找到了一段代码如下: #使用第三方库BeautifulSou...

使用Python编写爬虫的基本模块及框架使用指南

基本模块  python爬虫,web spider。爬取网站获取网页数据,并进行分析提取。 基本模块使用的是 urllib,urllib2,re,等模块 基本用法,例子: (1...

python3爬虫获取html内容及各属性值的方法

今天用到BeautifulSoup解析爬下来的网页数据 首先导入包from bs4 import BeautifulSoup 然后可以利用urllib请求数据 记得要导包 impor...