怎样抓取知乎问题下的所有回答中的所有图片
http://lilydjwg.is-programmer.com/2015/5/30/save-images-with-mitmproxy.95114.html
■网友
给你来个不是python的思路
看一下话题采集里面,能帮你采出图片的链接,是不是你要的
■网友
根据 通过Python爬虫爬取某个问题下的图片 和 Python爬虫学习笔记一:简单网页图片抓取 这两个示例结合测试了下, 其实不需要登录也是可以下载的.# -*-coding:utf-8 -*-import reimport urllibimport urllib2import osdef getHtml(url): page = urllib.urlopen(url) html = page.read() return htmldef mkdir(path): path = path.strip() isExists = os.path.exists(path) if not isExists: print u\u0026#39;新建了名字叫做\u0026#39;,path,u\u0026#39;的文件夹\u0026#39; os.makedirs(path) return True else: print u\u0026#39;名为\u0026#39;,path,u\u0026#39;的文件夹已经创建成功\u0026#39; return Falsedef saveImages(imglist,name): number = 1 for imageURL in imglist: splitPath = imageURL.split(\u0026#39;.\u0026#39;) fTail = splitPath.pop() if len(fTail) \u0026gt; 3: fTail = \u0026#39;jpg\u0026#39; fileName = name + "/" + str(number) + "." + fTail # 对于每张图片地址,进行保存 try: u = urllib2.urlopen(imageURL) data = https://www.zhihu.com/api/v4/questions/34867032/u.read() f = open(fileName,/u0026#39;wb+/u0026#39;) f.write(data) print u/u0026#39;正在保存的一张图片为/u0026#39;,fileName f.close() except urllib2.URLError as e: print (e.reason) number += 1def getAllImg(html): #利用正则表达式把源代码中的图片地址过滤出来 # reg = r/u0026#39;src="(https://.+?\\.jpg)"\u0026#39; # reg = r\u0026#39;data-actualsrc="https://www.zhihu.com/api/v4/questions/34867032/(https?://pic.+?\\.jpg|png|jpeg)"\u0026#39; reg = r\u0026#39;data-actualsrc="https://www.zhihu.com/api/v4/questions/34867032/(.*?)"\u0026gt;\u0026#39; imgre = re.compile(reg, re.S) imglist = imgre.findall(html) #表示在整个网页中过滤出所有图片的地址,放在imglist中 return imglistif __name__ == \u0026#39;__main__\u0026#39;: html = getHtml("https://www.zhihu.com/question/35242408") path = u\u0026#39;D://图片/\u0026#39; mkdir(path) #创建本地文件夹 imglist = getAllImg(html) #获取图片的地址列表 saveImages(imglist,path) # 保存图片执行等待一段时间之后, 就可以在本地看到图片信息了.
■网友
看我的github,里面python爬虫实现了你需要的功能…(大牛们请忽略)
■网友
现在是2017年6月15日 答案下应该是ajax方式加载,但是答案接口的请求方式却已经不是post,是get方式,接口能找到,浏览器F12下点击问题网页中查看更多答案后新出来的那个请求,
【怎样抓取问题下的所有回答中的所有图片】 比如以下
https://www.zhihu.com/api/v4/questions/38140937/answers?sort_by=default\u0026amp;amp;include=data%5B%2A%5D.is_normal%2Cis_collapsed%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Cmark_infos%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%2Cupvoted_followees%3Bdata%5B%2A%5D.author.follower_count%2Cbadge%5B%3F%28type%3Dbest_answerer%29%5D.topics\u0026amp;amp;limit=20\u0026amp;amp;offset=23只需要构建request对象以get方式向这个页面发送请求,再接收响应的json数据(json.load),解析后在json数据里查找图片链接,ok了.如果要抓取所有答案,接口的最后一个参数offset要设置循环来递增就行.一开始我也只能抓取3个回答
推荐阅读
- 聪明人养花,这3种“花”怎样也要养一盆,每年能省不少医药费
- 互联网怎样解决“家政服务上门速度慢”的问题
- 怎样看待从1月8号起,QQ钱包开始提现收费
- 银行it人怎样转型
- 为啥知乎上普便有一种【我在北上广深打工,所以拥有更好的视野】这样的错觉
- 汽车|冬天怎样让车内温度快速升高?座椅加热的最佳使用方式二,外循环的作用总结
- 怎样进入通信行业
- 怎样评价扶他柠檬茶的小说《云养汉》的结尾
- 怎样成为一名合格的Python程序员?
- 知乎有没有必要增加一个特别关注功能
