怎样用Python 写一个爬图片的程序?
2013-03-27 17:18
351 查看
在V2EX上看到一个问题"怎样用Python 写一个爬图片的程序?"http://www.v2ex.com/t/61686#reply18点击打开链接
需求:我很喜欢杉本有美大妹子,想从贴吧抓取她的全部写真照,这样就不用一张张右键,我该怎么一步步做呢?
http://tieba.baidu.com/p/2166231880
Ubuntu12.04系统:
代码:https://gist.github.com/anonymous/5066411点击打开链接
downloadimages.py
执行:
python downloadimages.py
去目录里看看妹子图片吧....
------------------------------------------------------------------------------
Windows下运行该代码:
1、安装相关包(注意版本号):cmd 下 easy_install
lxml==2.3
2、Windows二进制安装(也可以采用此方法):http://www.lfd.uci.edu/~gohlke/pythonlibs/
点击打开
链接
3、运行 downloadimages.py,图片就下载下来了。-:)
----------------------------------------------------------------------------------
使用urllib和re库也可以解决问题:
代码如下:
#! /usr/bin/env python
#coding:utf-8
import urllib,re
def get_html(url):
page = urllib.urlopen(url)
html = page.read()
return html
def get_img(html):
reg = r'src="(.*?\.jpg)" bdwater='
imgre = re.compile(reg)
imglist = re.findall(imgre, html)
i = 0
for imgurl in imglist:
urllib.urlretrieve(imgurl, '%s.jpg'%i)
i+=1
html = get_html('http://tieba.baidu.com/p/2166231880')
print get_img(html)
需求:我很喜欢杉本有美大妹子,想从贴吧抓取她的全部写真照,这样就不用一张张右键,我该怎么一步步做呢?
http://tieba.baidu.com/p/2166231880
Ubuntu12.04系统:
1.安装:sudo easy_install-2.7 -U pip 2.安装:sudo pip-2.7 install requests 3.安装:sudoeasy_install lxml
第3步会报错:
ERROR: /bin/sh: 1: xslt-config: not found
** make sure the development packages of libxml2 and libxslt are installed **
解决方法:google search "ubuntu install python lxml"
安装:sudo apt-get install python-lxml
代码:https://gist.github.com/anonymous/5066411点击打开链接
downloadimages.py
import requests import lxml.html page = requests.get('http://tieba.baidu.com/p/2166231880').text doc = lxml.html.document_fromstring(page) for idx, el in enumerate(doc.cssselect('img.BDE_Image')): with open('%03d.jpg' % idx, 'wb') as f: f.write(requests.get(el.attrib['src']).content)
执行:
python downloadimages.py
去目录里看看妹子图片吧....
------------------------------------------------------------------------------
Windows下运行该代码:
1、安装相关包(注意版本号):cmd 下 easy_install
lxml==2.3
2、Windows二进制安装(也可以采用此方法):http://www.lfd.uci.edu/~gohlke/pythonlibs/
点击打开
链接
3、运行 downloadimages.py,图片就下载下来了。-:)
----------------------------------------------------------------------------------
使用urllib和re库也可以解决问题:
代码如下:
#! /usr/bin/env python
#coding:utf-8
import urllib,re
def get_html(url):
page = urllib.urlopen(url)
html = page.read()
return html
def get_img(html):
reg = r'src="(.*?\.jpg)" bdwater='
imgre = re.compile(reg)
imglist = re.findall(imgre, html)
i = 0
for imgurl in imglist:
urllib.urlretrieve(imgurl, '%s.jpg'%i)
i+=1
html = get_html('http://tieba.baidu.com/p/2166231880')
print get_img(html)
相关文章推荐
- 怎样用Python3 写一个爬图片的程序?
- Python 练习册,每天一个小程序----------000 图片右上角加上红色的数字
- 随便写的一个python小程序,从网页中下载图片
- 昨天晚上写了个 Python 程序,下载了一个福利网站 5000 来张妹子图片
- 第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-) Python3 图片 爬取 下载 到 本地
- 发一个图片合并python程序
- Python每日练习 10 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-)
- 假如我是一个Python编程高手,我会做一个怎样的程序呢?
- 一个简单的将yuv图片转化为jpeg格式图片的Python脚本程序
- 怎样在一个单文档程序中添加树形控件
- 一个用python和django写的小程序
- 一个简单的 python 实现 图片同步脚本 ,对于有图片群集的项目有参考价值
- 某天晚上要处理的800 张表的一个demo python小程序
- 原创:老司机为教老婆学Python,星期天用Python写了一个俄罗斯方块程序。现在分享给大家,供大家闲暇时娱乐。
- python cgi ajax - 使用CGIHTTPServer实现一个ajax程序
- 完整的写一个Python小程序
- windows下 python 关于管通道(程序回显)处理的一个小例子
- Python 练习冊,每天一个小程序
- Python脚本初窥——教你写一个文字处理脚本程序
- 一个计算电脑屏幕宽高的Python程序