无意中发现贴吧也出了个漂流瓶的东西,随手翻了翻发现居然有好多妹子图,闲来无事于是就想写个爬虫程序把图片全部抓取下来。

这里是贴吧漂流瓶地址
http://tieba.baidu.com/bottle...

1.分析

首先打开抓包神器 Fiddler ,然后打开漂流瓶首页,加载几页试试,在Fiddler中过滤掉图片数据以及非 http 200 状态码的干扰数据后,发现每一页的数据获取都很有规律,这就给抓取提供了便利。具体获取一页内容的url如下:

http://tieba.baidu.com/bottle...

看参数很容易明白,page_number 就是当前页码,page_size 就是当前页中包含的漂流瓶数量。

访问后得到的是一个json格式的数据,结构大致如下:

{"error_code": 0,"error_msg": "success","data": {"has_more": 1,"bottles": [{"thread_id": "5057974188","title": "美得不可一世","img_url": "http://imgsrc.baidu.com/forum/pic/item/a8c87dd062d9f2d3f0113c2ea0ec8a136227cca9.jpg"},{"thread_id": "5057974188","title": "美得不可一世","img_url": "http://imgsrc.baidu.com/forum/pic/item/a8c87dd062d9f2d3f0113c2ea0ec8a136227cca9.jpg"},...}
}

内容很直白一眼就看出,bottles 中的数据就是我们想要的(thread_id 瓶子具体id, title 妹纸吐槽的内容, img_url 照片真实地址),遍历 bottles 就可以获得当前页的所有漂流瓶子。(其实现在得到的只是封面图哦,打开具体的瓶子有惊喜,因为我比较懒就懒得写了,不过我也分析了内部的数据,具体url是:http://tieba.baidu.com/bottle...瓶子thread_id>)

还有一个参数 has_more 猜测是是否存在下一页的意思。
到这里采集方式应该可以确定了。就是从第一页不停往后循环采集,直到 has_more 这个参数不为 1 结束。

2.编码

这里采用的是 python2.7 + urllib2 + demjson 来完成此项工作。urllib2 是python2.7自带的库,demjson 需要自己安装下(一般情况下用python自带的json库就可以完成json解析任务,但是现在好多网站提供的json并不规范,这就让自带json库无能为力了。)

demjson 安装方式 (windows 不需要 sudo)

sudo pip install demjson

或者

sudo esay_install demjson

2.1获得一页内容

def bottlegen():page_number = 1while True:try:data = urllib2.urlopen("http://tieba.baidu.com/bottle/bottles?page_number=%d&page_size=30" % page_number).read()json = demjson.decode(data)if json["error_code"] == 0:data = json["data"]has_more = data["has_more"]bottles = data["bottles"]for bottle in bottles:thread_id = bottle["thread_id"]title = bottle["title"]img_url = bottle["img_url"]yield (thread_id, title, img_url)if has_more != 1:breakpage_number += 1except:raiseprint("bottlegen exception")time.sleep(5)

这里使用python的生成器来源源不断的输出分析到的内容。

2.2根据url保存图片数据

for thread_id, title, img_url in bottlegen():filename = os.path.basename(img_url)pathname = "tieba/bottles/%s_%s" % (thread_id, filename)print filenamewith open(pathname, "wb") as f:f.write(urllib2.urlopen(img_url).read())f.close()

2.3全部代码如下

# -*- encoding: utf-8 -*-
import urllib2
import demjson
import time
import re
import osdef bottlegen():page_number = 1while True:try:data = urllib2.urlopen("http://tieba.baidu.com/bottle/bottles?page_number=%d&page_size=30" % page_number).read()json = demjson.decode(data)if json["error_code"] == 0:data = json["data"]has_more = data["has_more"]bottles = data["bottles"]for bottle in bottles:thread_id = bottle["thread_id"]title = bottle["title"]img_url = bottle["img_url"]yield (thread_id, title, img_url)if has_more != 1:breakpage_number += 1except:raiseprint("bottlegen exception")time.sleep(5)def imggen(thread_id):try:data = urllib2.urlopen("http://tieba.baidu.com/bottle/photopbPage?thread_id=%s" % thread_id).read()match = re.search(r"\_\.Module\.use\(\'encourage\/widget\/bottle\',(.*?),function\(\)\{\}\);", data)data = match.group(1)json = demjson.decode(data)json = demjson.decode(json[1].replace("\r\n", ""))for i in json:thread_id = i["thread_id"]text = i["text"]img_url = i["img_url"]yield (thread_id, text, img_url)except:raiseprint("imggen exception")try:os.makedirs("tieba/bottles")
except:passfor thread_id, _, _ in bottlegen():for _, title, img_url in imggen(thread_id):filename = os.path.basename(img_url)pathname = "tieba/bottles/%s_%s" % (thread_id, filename)print filenamewith open(pathname, "wb") as f:f.write(urllib2.urlopen(img_url).read())f.close()

运行后会先获得每页所有瓶子,然后再获得具体瓶子中的所有图片,输出到 tieba/bottles/xxxxx.jpg 中。(因为比较懒就没做错误兼容,见谅 ^_^,,,)

结论

结论是,,, 都是骗人的就首页有几张好看的 - -,,, 他喵的,,,

最后贴下采集成果

使用python抓取百度漂流瓶妹纸照片相关推荐

  1. Python爬虫之小试牛刀——使用Python抓取百度街景图像

    之前用.Net做过一些自动化爬虫程序,听大牛们说使用python来写爬虫更便捷,按捺不住抽空试了一把,使用Python抓取百度街景影像. 这两天,武汉迎来了一个德国总理默克尔这位大人物,又刷了一把武汉 ...

  2. python爬取地图地址_用Python抓取百度地图里的店名,地址和联系方式

    原标题:用Python抓取百度地图里的店名,地址和联系方式 每日干货好文分享丨请点击+关注 欢迎关注天善智能微信公众号,我们是专注于商业智能BI,大数据,数据分析领域的垂直社区. 对商业智能BI.大数 ...

  3. python语言翻译-教你用Python抓取百度翻译

    最近一直在一个平台学习Python,所以分享下课程里面抓取百度翻译的操作.原理其实也很简单,就是将浏览器请求的操作我们用python进行模拟,从而获取到返回的数据,我们将返回的数据进行提取,从而得到我 ...

  4. 教你用Python抓取百度翻译

    最近一直在一个平台学习Python,所以分享下课程里面抓取百度翻译的操作.原理其实也很简单,就是将浏览器请求的操作我们用python进行模拟,从而获取到返回的数据,我们将返回的数据进行提取,从而得到我 ...

  5. Python抓取百度贴吧网页信息以及代码下载

    代码是抓取百度贴吧帖子的回复内容的.包括帖子标题.帖子回复数量,帖子页码,回复楼层.回复时间,也可以只查看楼主的回复信息.最后将获取到的帖子信息记录到记事本中. 具体结果看图: 上面的图片打印的是帖子 ...

  6. python抓取百度妹子图

    from pyquery import PyQuery as pq from lxml import etree 请求库 import requests count = 1 def Download_ ...

  7. python在线翻译脚本_用Python抓取百度翻译内容并打造自己的翻译脚本!

    英文不好一直是我的一个短板,尤其是在学习代码的阶段,经常需要查询各种错误,很是苦逼,一直就想自己做个翻译的脚本,省去打开网页的时间,但是查询之后发现网上的教程都是百度翻译改版之前的爬虫,不得已只好自己 ...

  8. python新闻关键词次数_使用python抓取百度搜索、百度新闻搜索的关键词个数

    由于实验的要求,需要统计一系列的字符串通过百度搜索得到的关键词个数,于是使用python写了一个相关的脚本. 在写这个脚本的过程中遇到了很多的问题,下面会一一道来. ps:我并没有系统地学习过pyth ...

  9. 如何用python抓地图_用Python抓取百度地图里的店名,地址和联系方式!屌不屌?...

    昨晚看到一篇爬取百度地图信息的的代码,我更改了城市,关键词,页码等,完成了获取有关"筛网"店铺的信息. 代码如下: import requests import re import ...

最新文章

  1. python做excel表格柱状图_用python处理excel数据(八)实现excel表中柱状图功能
  2. python系统-基于Python搭建Django后台管理系统
  3. mantis1.18升级1.2X方法
  4. Google Maps——页面提示[For development purposes only]解决方案
  5. wxWidgets:wxPropertyGrid类用法
  6. vmware智能资源调整
  7. android.os.log,android.os.Handler和java.util.logging.Handler之间的区别?
  8. 上海查处一批涉“樱桃直播”传播淫秽物品牟利案女主播 已有14人获刑
  9. webpack中的loader
  10. drool 7.x 属性 : no-loop
  11. Evernote推出实体笔记本Evernote Business Notebook,支持搜索手写笔记,用实体便签将笔记同步到Evernote上的相应分类
  12. 华为机考 叠积木 python (以及处理输入)
  13. BZOJ 4565 字符合并 (区间 DP 状压 DP)
  14. 基于 DirectShow 实现 SourceFilter 常见问题分析
  15. 基因和疾病:缺陷、脱轨以及妥协
  16. 关于如何打破传统阅读体验的一些想法
  17. 常见的NoSQL数据库介绍
  18. microsoft认证解说
  19. 汉字从拼音到文字的完整映射表
  20. 详解边缘计算系统逻辑架构:云、边、端协同

热门文章

  1. Json在Java后台的操作
  2. Android--Handler使用
  3. Ubuntu 下添加OpenERP command 快捷启动方式
  4. C# toolstrip 上添加DateTimePicker Control控件
  5. XPath实例教程十四、following-sibling轴
  6. 此为太阳历的技术支持博客
  7. 已知bug列表——Solidity中文文档(12)
  8. Ansible第一篇:基础
  9. LoadRunner监控mysql利器-SiteScope(转)
  10. Javascript 节点 全面解析