[python学习] 简单爬取图片站点图库中图片
近期老师让学习Python与维基百科相关的知识,无聊之中用Python简单做了个爬取“游讯网图库”中的图片,由于每次点击下一张感觉很浪费时间又繁琐。主要分享的是怎样爬取HTML的知识和Python怎样下载图片。希望对大家有所帮助,同一时候发现该站点的图片都挺精美的,建议阅读原网下载图片,支持游讯网不要去破坏它。
通过浏览游讯网发现它的图库URL为。当中所有图片为0_0_1到0_0_75:
http://pic.yxdown.com/list/0_0_1.html
http://pic.yxdown.com/list/0_0_75.html
(需在本地创建E:\\Picture3目录和Python执行目录创建yxdown目录)
# coding=utf-8
# 声明编码方式 默认编码方式ASCII 參考https://www.python.org/dev/peps/pep-0263/
import urllib
import time
import re
import os '''
Python下载游迅网图片 BY:Eastmount
''' '''
**************************************************
#第一步 遍历获取每页相应主题的URL
#http://pic.yxdown.com/list/0_0_1.html
#http://pic.yxdown.com/list/0_0_75.html
**************************************************
'''
fileurl=open('yxdown_url.txt','w')
fileurl.write('****************获取游讯网图片URL*************\n\n')
#建议num=3 while num<=3一次遍历一个页面全部主题,下次换成num=4 while num<=4而不是1-75
num=3
while num<=3:
temp = 'http://pic.yxdown.com/list/0_0_'+str(num)+'.html'
content = urllib.urlopen(temp).read()
open('yxdown_'+str(num)+'.html','w+').write(content)
print temp
fileurl.write('****************第'+str(num)+'页*************\n\n') #爬取相应主题的URL
#<div class="cbmiddle"></div>中<a target="_blank" href="/html/5533.html" >
count=1 #计算每页1-75中详细网页个数
res_div = r'<div class="cbmiddle">(.*? )</div>'
m_div = re.findall(res_div,content,re.S|re.M)
for line in m_div:
#fileurl.write(line+'\n')
#获取每页全部主题相应的URL并输出
if "_blank" in line: #防止获取列表list/1_0_1.html list/2_0_1.html
#获取主题
fileurl.write('\n\n********************************************\n')
title_pat = r'<b class="imgname">(.*? )</b>'
title_ex = re.compile(title_pat,re.M|re.S)
title_obj = re.search(title_ex, line)
title = title_obj.group()
print unicode(title,'utf-8')
fileurl.write(title+'\n')
#获取URL
res_href = r'<a target="_blank" href="(.*? )"'
m_linklist = re.findall(res_href,line)
#print unicode(str(m_linklist),'utf-8')
for link in m_linklist:
fileurl.write(str(link)+'\n') #形如"/html/5533.html" '''
**************************************************
#第二步 去到详细图像页面 下载HTML页面
#http://pic.yxdown.com/html/5533.html#p=1
#注意先本地创建yxdown 否则报错No such file or directory
**************************************************
'''
#下载HTML网页无原图 故加'#p=1'错误
#HTTP Error 400. The request URL is invalid.
html_url = 'http://pic.yxdown.com'+str(link)
print html_url
html_content = urllib.urlopen(html_url).read() #详细站点内容
#可凝视它 暂不下载静态HTML
open('yxdown/yxdown_html'+str(count)+'.html','w+').write(html_content) '''
#第三步 去到图片界面下载图片
#图片的链接地址为http://pic.yxdown.com/html/5530.html#p=1 #p=2
#点击"查看原图"HTML代码例如以下
#<a href="javascript:;" style=""onclick="return false;">查看原图</a>
#通过JavaScript实现 并且该界面存储全部图片链接<script></script>之间
#获取"original":"http://i-2.yxdown.com/2015/3/18/6381ccc..3158d6ad23e.jpg"
'''
html_script = r'<script>(.*? )</script>'
m_script = re.findall(html_script,html_content,re.S|re.M)
for script in m_script:
res_original = r'"original":"(.*? )"' #原图
m_original = re.findall(res_original,script)
for pic_url in m_original:
print pic_url
fileurl.write(str(pic_url)+'\n') '''
#第四步 下载图片
#假设浏览器存在验证信息如维基百科 需加入例如以下代码
class AppURLopener(urllib.FancyURLopener):
version = "Mozilla/5.0"
urllib._urlopener = AppURLopener()
#參考 http://bbs.csdn.net/topics/380203601
#http://www.lylinux.org/python使用多线程下载图片.html
'''
filename = os.path.basename(pic_url) #去掉文件夹路径,返回文件名称
#No such file or directory 须要先创建文件Picture3
urllib.urlretrieve(pic_url, 'E:\\Picture3\\'+filename)
#http://pic.yxdown.com/html/5519.html
#IOError: [Errno socket error] [Errno 10060] #仅仅输出一个URL 否则输出两个同样的URL
break #当前页详细内容个数加1
count=count+1
time.sleep(0.1)
else:
print 'no url about content' time.sleep(1)
num=num+1
else:
print 'Download Over!!!'
1.简单遍历站点。获取每页相应主题的URL。当中每页都有无数个主题。当中主题的格式例如以下:
<!-- 第一步 爬取的HTML代码例如以下 -->
<div class="conbox">
<div class="cbtop">
</div>
<div class="cbmiddle">
<a target="_blank" href="/html/5533.html" class="proimg">
<img src="http://i-2.yxdown.com/2015/3/19/KDE5Mngp/a78649d0-9902-4086-a274-49f9f3015d96.jpg" alt="Miss大小姐驾到! 细数《英雄联盟》圈的电竞女神" />
<strong></strong>
<p>
<span>b></b>1836人看过</span>
<em><b></b>10张</em>
</p>
<b class="imgname">Miss大小姐驾到! 细数《英雄联盟》圈的电竞女神</b>
</a>
<a target="_blank" href="/html/5533.html" class="plLink"><em>1</em>人评论</a>
</div>
<div class="cbbottom">
</div>
<a target="_blank" class="plBtn" href="/html/5533.html"></a>
</div>
它是由无数个<div class="conbox"></div>组成,当中我们仅仅须要提取<a target="_blank" href="/html/5533.html" class="proimg">中的href就可以,然后通过URL拼接实现到详细的主题页面。当中相应上面的布局例如以下图所看到的:
http://pic.yxdown.com/html/5533.html#p=1
同一时候下载本地HTML页面能够凝视该句代码。此时须要点击“查看图片”才干下载原图。点击右键仅仅能另存为站点html。
但我发现它是通过JavaScript实现的浏览,即:
<a href="javascript:;" onclick="return false;" id="photoOriginal">查看原图</a>
同一时候它把全部图片都写在以下代码<script></script>中:
<script>var images = [
{ "big":"http://i-2.yxdown.com/2015/3/18/KDkwMHgp/6381ccc0-ed65-4422-8671-b3158d6ad23e.jpg",
"thumb":"http://i-2.yxdown.com/2015/3/18/KHgxMjAp/6381ccc0-ed65-4422-8671-b3158d6ad23e.jpg",
"original":"http://i-2.yxdown.com/2015/3/18/6381ccc0-ed65-4422-8671-b3158d6ad23e.jpg",
"title":"","descript":"","id":75109},
{ "big":"http://i-2.yxdown.com/2015/3/18/KDkwMHgp/fec26de9-8727-424a-b272-f2827669a320.jpg",
"thumb":"http://i-2.yxdown.com/2015/3/18/KHgxMjAp/fec26de9-8727-424a-b272-f2827669a320.jpg",
"original":"http://i-2.yxdown.com/2015/3/18/fec26de9-8727-424a-b272-f2827669a320.jpg",
"title":"","descript":"","id":75110},
...
</script>
当中获取原图-original就可以,缩略图-thumb,大图-big,通过正則表達式下载URL:
res_original = r'"original":"(.*?)"' #原图
m_original = re.findall(res_original,script)
4.最后一步就是下载图片。当中我不太会使用线程,仅仅是简单加入了time.sleep(0.1) 函数。下载图片可能会遇到维基百科那种訪问受限。须要对应设置。核心代码例如以下:
import os
import urllib
class AppURLopener(urllib.FancyURLopener):
version = "Mozilla/5.0"
urllib._urlopener = AppURLopener()
url = "http://i-2.yxdown.com/2015/2/25/c205972d-d858-4dcd-9c8b-8c0f876407f8.jpg"
filename = os.path.basename(url)
urllib.urlretrieve(url , filename)
同一时候我也在本地创建目录Picture3,并txt记录获取的URL,例如以下图所看到的:
(By:Eastmount 2015-3-20 下午5点 http://blog.csdn.net/eastmount/)
[python学习] 简单爬取图片站点图库中图片的更多相关文章
- [Python学习] 简单爬取CSDN下载资源信息
这是一篇Python爬取CSDN下载资源信息的样例,主要是通过urllib2获取CSDN某个人全部资源的资源URL.资源名称.下载次数.分数等信息.写这篇文章的原因是我想获取自己的资源全部的评论信息. ...
- Python爬虫--简单爬取图片
今天晚上弄了一个简单的爬虫,可以爬取网页的图片,现在现在做一下准备工作. 需要的库:urllib 和 re urllib库可以理解为是一个url下载器,其中有三个重要的方法 urllib.urlope ...
- python学习之----爬取图片
import os from urllib.request import urlretrieve from urllib.request import urlopen from bs4 import ...
- 14.python案例:爬取电影天堂中所有电视剧信息
1.python案例:爬取电影天堂中所有电视剧信息 #!/usr/bin/env python3 # -*- coding: UTF-8 -*- '''======================== ...
- Python爬虫学习 - day1 - 爬取图片
利用Python完成简单的图片爬取 最近学习到了爬虫,瞬时觉得很高大上,想取什么就取什么,感觉要上天.这里分享一个简单的爬取汽车之家文章列表的图片教程,供大家学习. 需要的知识点储备 本次爬虫脚本依赖 ...
- python 使用for循环简单爬取图片(1)
现在的网站大多做了反爬处理,找一个能爬的网站还真不容易. 下面开始一步步实现: 1.简单爬录目图片 import urllib.request import re def gethtml(url): ...
- [Python学习] 简单网络爬虫抓取博客文章及思想介绍
前面一直强调Python运用到网络爬虫方面很有效,这篇文章也是结合学习的Python视频知识及我研究生数据挖掘方向的知识.从而简介下Python是怎样爬去网络数据的,文章知识很easy ...
- python +requests 爬虫-爬取图片并进行下载到本地
因为写12306抢票脚本需要用到爬虫技术下载验证码并进行定位点击所以这章主要讲解,爬虫,从网页上爬取图片并进行下载到本地 爬虫实现方式: 1.首先选取你需要的抓取的URL:2.将这些URL放入待抓 ...
- Python多线程Threading爬取图片,保存本地,openpyxl批量插入图片到Excel表中
之前用过openpyxl库保存数据到Excel文件写入不了,换用xlsxwriter 批量插入图片到Excel表中 1 import os 2 import requests 3 import re ...
随机推荐
- nodejs递归创建目录
var fs = require("fs"); var path = require("path"); // 递归创建目录 异步方法 function mkdi ...
- ++x和x++
#include <stdio.h> int main() { int a,b,c=1,d=1; a = c++; b = ++d; printf("%d\t%d\n" ...
- DB2数据库报 [SQL0805N Package "NULLID.SQLLD003" was not found.]
解决办法: cd /home/db2inst1/sqllib/bnddb2 bind @db2cli.lst blocking all grant public sqlerror continue C ...
- 牛客练习赛10 E题 数列查找 (分块思想 + 莫队算法)
题目链接 数列查找 考虑分块然后跑莫队, 设$c[i]$为$i$在当前维护的区间内出现的次数, $g[i]$为在当前维护的区间内有多少个数出现次数为$i$, $bg[i]$把出现次数分块,$bg[i ...
- C++ | class size
c++类大小和机器还有编译器有关.64位机器指针大小为8个字节,32位机器为4个字节. 每个实例在内存中都有一个独一无二的地址,为了达到这个目的,编译器往往会给一个空类隐含的加一个字节,这样空类在实例 ...
- bzoj 5123: [Lydsy1712月赛]线段树的匹配
设f[0/1][x]为区间[1,x]的根向下 不选(0)或者选(1) 的dp pair<最优值,方案数>. 可以很容易的发现总状态数就是log级别的,因为2*n 与 (2*n+1 或者 ...
- Jenkins强制设置语言为中文
解决方法如下: 1.使用中文版的google浏览器,并确定把语言设置成了中文.语言配置在设置页.如下: 2.直接设置jenkins的语言.强制性. 前提:先安装插件:Locale plugin 配置如 ...
- 【IntelliJ IDEA】代码中出现Usage of API documented as @since 1.8+ more..
在idea中写代码过程中.有这种报错出现: Usage of API documented as @since 1.8+ more.. 修改JDK版本的几个地方 最后,在pom.xml文件中添加: & ...
- PHP生成月历代码
<?php /* Function Written by Nelson Neoh @3/2004. For those who wants to utilize this code, p ...
- Git的提交忽略文件
.gitingore文件内容如下 /target//.settings//.classpath/.project/logs/