Python 爬虫学习 网页图片下载
2014-10-21 21:05
851 查看
使用正则表达式匹配
使用第三方库BeautifulSoup匹配
# coding:utf-8 import re import urllib def get_content(url): """ Evilxr, """ html = urllib.urlopen(url) content = html.read() html.close() return content def get_images(info): """" Download Baidu pictures. <img class="BDE_Image" src="http:*****"> """ regex = r' class="BDE_Image" src="(.+?\.jpg)" ' pat = re.compile(regex) images_code = re.findall(pat, info) i = 0 for image_url in images_code: print image_url urllib.urlretrieve(image_url, '%s.jpg' % i) i = i +1 print len(images_code) info = get_content("http://tieba.baidu.com/p/2299704181") print get_images(info)
使用第三方库BeautifulSoup匹配
# 安装 sudo pip install beautifulsoup4
# coding:utf-8 import urllib from bs4 import BeautifulSoup def get_content(url): """ Evilxr, """ html = urllib.urlopen(url) content = html.read() html.close() return content def get_images(info): """ 使用BeautifulSoup在网页源码中匹配图片地址 """ soup = BeautifulSoup(info) all_img = soup.find_all('img', class_="BDE_Image" ) i = 1 for img in all_img: print img['src'] urllib.urlretrieve(img['src'], '%s.jpg' % i) i = i +1 print "一共下载了 ", len(all_img), "张图片" info = get_content("http://tieba.baidu.com/p/3368845086") print get_images(info)
相关文章推荐
- python爬虫:下载百度贴吧图片(多页)学习笔记
- [python][爬虫]从网页中下载图片
- python学习笔记(12)--爬虫下载煎蛋网图片
- python学习笔记(11)--爬虫下载漫画图片
- Python爬虫学习笔记二:百度贴吧网页图片抓取
- python学习笔记(14)--爬虫下载漫画图片修改版
- Python3.x爬虫下载网页图片的实例讲解
- 【Python3.6爬虫学习记录】(一)爬取简单的静态网页图片
- 【Python】python3实现网页爬虫下载图片
- python爬虫框架scrapy学习图片下载
- python学习:urllib库学习:制作简易爬虫下载图片
- python学习笔记(8)--爬虫下载占位图片
- Python爬虫学习笔记一:简单网页图片抓取
- python学习笔记(10)--爬虫下载煎蛋图片
- Python爬虫学习记录(1)——百度贴吧图片下载
- python爬虫:下载百度贴吧图片学习笔记
- python 网页爬虫,下载网络图片
- Python学习笔记:爬虫下载图片
- python爬虫一:必应图片(从网页源代码中找出图片链接然后下载)
- Python 爬虫5——爬取并下载网页指定规格的图片