<?xml version="1.0" encoding="utf-8" standalone="yes"?><?xml-stylesheet type="text/css" href="https://imnerd.org/css/rss.css"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>爬虫 on 怡红院落</title><link>https://imnerd.org/tags/%E7%88%AC%E8%99%AB.html</link><description>Recent content in 爬虫 on 怡红院落</description><language>zh-CN</language><copyright>© 2021</copyright><lastBuildDate>Sun, 15 Jun 2014 09:42:00 +0800</lastBuildDate><atom:link href="https://imnerd.org/tags/%E7%88%AC%E8%99%AB/index.xml" rel="self" type="application/rss+xml"/><item><title>Tumblr整站图片下载</title><link>https://imnerd.org/tumblr-blog-all-photo-download.html</link><pubDate>2014-09-15</pubDate><guid>https://imnerd.org/tumblr-blog-all-photo-download.html</guid><description>前言 前几天闲着无聊没事（其实是一大堆事摆在面前作死不愿意做）把Python的入门教程又看了一遍，感觉这样做好没有效率啊，遂想拿个东西练练手。Python做好的练手项目就是爬虫了（我也不知道从哪里看到的，反正就有这么一说），正好前两天看了Tumblr的一个图片博客，然后网速渣到爆表，遂想干脆给整站图片都抓下来好了。
说干就干篇 基本的思路非常简单啦，无非就是把页面抓下来然后解析里面的IMG标签得到图片地址最后下载下来就好叻。感谢Tumblr没有对IP访问频率进行限制让我少做了不少工作，ありがとう~
首先要说明Python版本啦，我的版本是Python3.4.1，对2.x不兼容。不过索性代码也非常的简单，无非就是urllib库的问题，稍加修改也是可以转换过去的，这个我就不多表述啦。另外就是我的代码是以http://triketora.tumblr.com/这个博客为基准的，因为每个博客的DOM标签可能都不太一样，所以没法做到代码通用，需要根据博客的主题做相应的修改。
页面的抓取我是直接用的urllib.request库，简单粗暴而且非常有效。页面DOM的解析我使用的当然是大名鼎鼎的BeautifulSoup了。至于BeautifulSoup的安装同样不多做表述，其中文文档可参见Beautiful Soup 4.2.0 文档。至于最后一步的图片下载我使用的是urllib.request.urlretrieve函数，这函数简直就是爬虫利器啊有么有，简单快捷高效的就把图片给抓取下来了，简直好顶赞！
实战代码篇 #! /usr/bin/python3 import os, sys, urllib from urllib import request, parse from bs4 import BeautifulSoup def basename(url): return os.path.basename(parse.urlparse(url).path) def download(url, file): &amp;#39;&amp;#39;&amp;#39;download file&amp;#39;&amp;#39;&amp;#39; colWidth = 50 __perLen = 0 def reporthook(a, b, c): nonlocal __perLen per = (100*a*b)/c if per&amp;gt;100: per=100 realCol = int(per/100 * colWidth) down = &amp;#39;=&amp;#39;*(realCol-1) undown = &amp;#39; &amp;#39;*(colWidth-realCol) process = &amp;#39;[&amp;#39;+down+&amp;#39;&amp;gt;&amp;#39;+undown+&amp;#39;]&amp;#39; per = &amp;#39;{:.2f}%&amp;#39;.format(per) print(&amp;#39;\b&amp;#39;*__perLen, per+&amp;#39; &amp;#39;+process, end=&amp;#39; &amp;#39;, flush=True) __perLen = len(per)+len(process)+4 print(&amp;#39;Downloading URL File:\t%s&amp;#39; % (url)) urllib.request.urlretrieve(url, file, reporthook) print(&amp;#39;Downloaded!&amp;#39;) def downImages(post): for image in post.find_all(&amp;#39;img&amp;#39;): filename = basename(image[&amp;#39;src&amp;#39;]) if(os.path.exists(filename)): print(filename+&amp;#39; exists!&amp;#39;) continue download(image[&amp;#39;src&amp;#39;], filename) def spider(url): global start print(&amp;#34;It has ready to parse URL: &amp;#34;+url) html = BeautifulSoup( request.urlopen(url).read() ) for post in html.select(&amp;#34;.post&amp;#34;): iframe = post.find(&amp;#39;iframe&amp;#39;) if(iframe): if &amp;#39;photoset&amp;#39; not in iframe[&amp;#39;class&amp;#39;]: continue #! True Video Post iframe = BeautifulSoup( request.urlopen(iframe[&amp;#39;src&amp;#39;]).read() ) downImages(iframe) else: downImages(post) start += 1 spider(&amp;#39;%s/page/%d&amp;#39; % (URL, start)) if(len(sys.argv) &amp;lt;= 1): sys.exit(&amp;#39;no tumblr blog url&amp;#39;) _, URL, *start = sys.argv start = int( start[0] if len(start)&amp;gt;0 else 1 ) spider(&amp;#39;%s/page/%d&amp;#39; % (URL, start)) HOW TO USE? 首先保证你安装了Python3，以及BeautifulSoup库，复制以上代码到Spider.py文件。然后命令行输入：
Python3 Spider.py http://triketora.tumblr.com/ 你还可以指定起始下载页数，比如：
Python3 Spider.py http://triketora.tumblr.com/ 30 将会从第30页开始下载。
友情提示 默认下载的地方是Spider.py的同级目录，所以找个大点的位置放它哦。
作者有话说之还不能完结篇 怎么说呢，这是我写的第一个Python爬虫代码，甚至是第一个真正意义上的Python代码，所以看起来会有点糙啦，欢迎各位吐槽。而且如何做到代码通用也是一个让我值得思考的问题，目前的思路颇为烦恼啊。
以上！</description></item></channel></rss>