使用python来批量抓取网站图片
今天"无意"看美女无意溜达到一个网站,发现妹子多多,但是可恨一个page只显示一张或两张图片,家里WiFi也难用,于是发挥"程序猿"的本色,写个小脚本,把图片扒下来再看,类似功能已有不少大师实现了,但本着学习锻炼的精神,自己折腾一遍,涨涨姿势!
先来效果展示下:

python代码:
# -*- coding:utf8 -*-
import urllib2
import re
import requests
from lxml import etree
import os def check_save_path(save_path):
try:
os.mkdir(save_path)
except:
pass def get_image_name(image_link):
file_name = os.path.basename(image_link)
return file_name def save_image(image_link, save_path):
file_name = get_image_name(image_link)
file_path = save_path + "\\" + file_name
print("准备下载%s" % image_link)
try:
file_handler = open(file_path, "wb")
image_handler = urllib2.urlopen(url=image_link, timeout=5).read()
file_handler.write(image_handler)
file_handler.closed()
except Exception, ex:
print(ex.message) def get_image_link_from_web_page(web_page_link):
image_link_list = []
print(web_page_link)
try:
html_content = urllib2.urlopen(url=web_page_link, timeout=5).read()
html_tree = etree.HTML(html_content)
print(str(html_tree))
link_list = html_tree.xpath('//p/img/@src')
for link in link_list:
# print(link)
if str(link).find("uploadfile"):
image_link_list.append("http://www.xgyw.cc/" + link)
except Exception, ex:
pass
return image_link_list def get_page_link_list_from_index_page(base_page_link):
try:
html_content = urllib2.urlopen(url=base_page_link, timeout=5).read()
html_tree = etree.HTML(html_content)
print(str(html_tree))
link_tmp_list = html_tree.xpath('//div[@class="page"]/a/@href')
page_link_list = []
for link_tmp in link_tmp_list:
page_link_list.append("http://www.xgyw.cc/" + link_tmp)
return page_link_list
except Exception, ex:
print(ex.message)
return [] def get_page_title_from_index_page(base_page_link):
try:
html_content = urllib2.urlopen(url=base_page_link, timeout=5).read()
html_tree = etree.HTML(html_content)
print(str(html_tree))
page_title_list = html_tree.xpath('//td/div[@class="title"]')
page_title_tmp = page_title_list[0].text
print(page_title_tmp)
return page_title_tmp
except Exception, ex:
print(ex.message)
return "" def get_image_from_web(base_page_link, save_path):
check_save_path(save_path)
page_link_list = get_page_link_list_from_index_page(base_page_link)
for page_link in page_link_list:
image_link_list = get_image_link_from_web_page(page_link)
for image_link in image_link_list:
save_image(image_link, save_path) base_page_link = "http://www.xgyw.cc/tuigirl/tuigirl1346.html"
page_title = get_page_title_from_index_page(base_page_link)
if page_title <> "":
save_path = "N:\\PIC\\" + page_title
else:
save_path = "N:\\PIC\\other\\" get_image_from_web(base_page_link, save_path)
代码思路:
使用urllib2.urlopen(url).open来获取页面数据,再使用etree.HTML()将页面解析成xml格式,方便使用xmlpath方式来获取特定node的值,最终遍历所有页面得到要下载的图片,将图片保存到本地。
--=========================================================
python包安装:
很多python包没有windows安装包,或者没有X64版本的安装包,对于新手来说,很难快速上手,可以使用pip或easy_install来安装要使用的安装包,相关安装方式:https://pypi.python.org/pypi/setuptools
本人采用easy_install方式,我电脑安装python2.7,安装路径为:C:\Python27\python.exe,下载ez_setup.py文件后到c盘保存,然后运行cmd执行以下命令:
C:\Python27\python.exe "c:\ez_setup.py"
即可安装easy_install,安装结束后可以C:\Python27\Scripts下看到easy_install-2.7.exe,如果我们想在本地安装requests包,那么可以运行以下命令来试下:
"C:\Python27\Scripts\easy_install-2.7.exe" requests
--==========================================================
依旧是妹子压贴,推女郎第68期,想要图的自己百度

使用python来批量抓取网站图片的更多相关文章
- Python入门-编写抓取网站图片的爬虫-正则表达式
//生命太短 我用Python! //Python真是让一直用c++的村里孩子长知识了! 这个仅仅是一个测试,成功抓取了某网站1000多张图片. 下一步要做一个大新闻 大工程 #config = ut ...
- python网络爬虫抓取网站图片
本文介绍两种爬取方式: 1.正则表达式 2.bs4解析Html 以下为正则表达式爬虫,面向对象封装后的代码如下: import urllib.request # 用于下载图片 import os im ...
- Python3利用BeautifulSoup4批量抓取站点图片的代码
边学边写代码,记录下来.这段代码用于批量抓取主站下所有子网页中符合特定尺寸要求的的图片文件,支持中断. 原理很简单:使用BeautifulSoup4分析网页,获取网页<a/>和<im ...
- php远程抓取网站图片并保存
以前看到网上别人说写程序抓取网页图片的,感觉挺神奇,心想什么时候我自己也写一个抓取图片的方法! 刚好这两天没什么事,就参考了网上一个php抓取图片代码,重点借鉴了 匹配img标签和其src属性正则的写 ...
- python爬虫批量抓取ip代理
使用爬虫抓取数据时,经常要用到多个ip代理,防止单个ip访问太过频繁被封禁.ip代理可以从这个网站获取:http://www.xicidaili.com/nn/.因此写一个python程序来获取ip代 ...
- 【Python】批量爬取网站URL测试Struts2-045漏洞
1.概述都懒得写了.... 就是批量测试用的,什么工具里扣出来的POC,然后根据自己的理解写了个爬网站首页URL的代码... #!/usr/bin/env python # -*- coding: u ...
- Python -- 网络编程 -- 抓取网页图片 -- 豆瓣妹子
首先分析页面URL,形如http://dbmeizi.com/category/[1-14]?p=[0-476] 图片种类对应编号: 1:'性感', 2:'有沟', 3:'美腿', 4:'小露点', ...
- Python -- 网络编程 -- 抓取网页图片 -- 图虫网
字符串(str)编码成字节码(bytes),字节码解码为字符串 获取当前环境编码:sys.stdin.encoding url编码urllib.parse.quote() url解码urllib.pa ...
- 【转】Python 代码批量抓取免费高清图片!
import requests from bs4 import BeautifulSoup import random import time from fake_useragent import U ...
随机推荐
- ApplicationContext(四)BeanFactory 功能扩展
ApplicationContext(四)BeanFactory 功能扩展 上节我们提到容器刷新的第二步初始化 BeanFactory 工厂并解析配制文件,但此时 BeanFactory 的功能还很简 ...
- Intellij idea 系列教程之常用配置项
Intellij idea 系列教程之常用配置项 Intellij idea 系列教程目录(https://www.cnblogs.com/binarylei/p/10347600.html) Lan ...
- Netty 源码 Channel(一)概述
Netty 源码 Channel(一)概述 Netty 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) Channel 为 Netty ...
- 通过java.util.Properties类来读取.properties文件中key对应的value
转:http://www.cnblogs.com/panjun-Donet/archive/2009/07/17/1525597.html
- walsh矩阵
矩阵A(1) 1 矩阵A(2) 1 1 1 -1 矩阵A(2n) 由上一级矩阵组合,即用上一级矩阵的一行生成本级矩阵的一行. 生成规则是 A(2n)[k] = { A(n)[k], A(n)[k] ...
- m序列
产生m序列移位寄存器是一种逻辑电路,1阶,2阶...的电路图各不相同. 一般使用本原多项式计算出各阶数电路图. 一般的多项式为 f(x) = c0 * x^0 + c1 * x^1 + c2 * x^ ...
- EASYUI DATAGRID 改变行值
在easyui datagrid 中如果要 改变当前选中行的值又不想用编辑状态,或者想从外部改变某一行的值,下面的方法可以做到 function test() { var ro ...
- Python开课复习7
操作系统 操作系统把复杂的硬件操作封装成简单的接口给用户/应用程序使用,其中文件就是操作系统提供给应用程序来操作硬盘虚拟概念,用户或应用程序通过操作文件,可以将自己的数据永久保存下来. #1. 打开文 ...
- idea如何将项目以eclipse保存
会生成 提交到svn eclipse 导入 首先使用TortoiseSVN下载要导入的项目 导入 已经存在的maven 项目 clean install -DskipTests t ...
- tomcat 服务器故障排除
故障现象 通过浏览器访问tomcat服务器发现服务器没有响应. 问题分析检查 登陆服务器发现,TOMCAT服务器并没有宕机,服务还在. 使用JPS命令查看了一下tomcat的进程ID,获取进程ID后, ...