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

yipeiwu_com6年前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爬虫信息输入及页面的切换方法

实现网页的键盘输入操作 from selenium.webdriver.common.keys import Keys 动态网页有时需要将鼠标悬停在某个元素上,相应的列表选项才能显...

python爬虫爬取淘宝商品信息

python爬虫爬取淘宝商品信息

本文实例为大家分享了python爬取淘宝商品的具体代码,供大家参考,具体内容如下 import requests as req import re def getHTMLT...

Python如何使用BeautifulSoup爬取网页信息

Python如何使用BeautifulSoup爬取网页信息

这篇文章主要介绍了Python如何使用BeautifulSoup爬取网页信息,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 简单爬取网...

Python3爬虫使用Fidder实现APP爬取示例

Python3爬虫使用Fidder实现APP爬取示例

之前爬取都是网页上的数据,今天要来说一下怎么借助Fidder来爬取手机APP上的数据。 一、环境配置 1、Fidder的安装和配置 没有安装Fidder软件的可以进入 这个网址 下载,...

python3之微信文章爬虫实例讲解

前提: python3.4 windows 作用:通过搜狗的微信搜索接口http://weixin.sogou.com/来搜索相关微信文章,并将标题及相关链接导入Excel表格中 说明:...