怎样抓取知乎问题下的所有回答中的所有图片( 三 )
import requests import os """下载方法download(src="", name="", path="",size=10240)"""def download(url="", name="", path="", size=10240): response = requests.request(method=\u0026#39;get\u0026#39;, url=url, stream=True) if path is not \u0026#39;/\u0026#39; : path += \u0026#39;/\u0026#39; total_path = path + name with open(total_path, mode=\u0026#39;w+b\u0026#39;) as f: for chunk in response.iter_content(1024): f.write(chunk) if os.path.getsize(total_path) \u0026lt; size : os.remove(total_path) """这是我以前写的通用下载模版(你打开一个网站什么都不点,你能看到的都可以下下来,包括大图).试了一下,是可以的,但是是分页显示的,隐藏那部分没办法......求大神告知,以前用过模拟预览器点击的方法(selenium),太麻烦了.
■网友
稍微修改了一下上面俩哥们的代码,就能跑通了。 @Jacob Jin @西瓜霜被人注册了 2019.01.30 # -*-coding:utf-8 -*-from urlparse import urlsplitfrom os.path import basenameimport urllib2import reimport requestsimport osimport jsonimport urllibdef 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 downloadImage(id ,path): number = 0 offset = 0 while offset \u0026lt; 1000: get_url = \u0026#39;https://www.zhihu.com/api/v4/questions/\u0026#39;+id+\u0026#39;/answers?include=data.is_normal,admin_closed_comment,reward_info,is_collapsed,annotation_action,annotation_detail,collapse_reason,is_sticky,collapsed_by,suggest_edit,comment_count,can_comment,content,editable_content,voteup_count,reshipment_settings,comment_permission,created_time,updated_time,review_info,relevant_info,question,excerpt,relationship.is_authorized,is_author,voting,is_thanked,is_nothelp;data.mark_infos.url;data.author.follower_count,badge.topics\u0026amp;limit=5\u0026amp;offset=\u0026#39;+str(offset)+\u0026#39;\u0026amp;sort_by=default\u0026#39; header = { \u0026#39;User-Agent\u0026#39;: "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:34.0) Gecko/20100101 Firefox/34.0", \u0026#39;Host\u0026#39;: "www.zhihu.com", } req = urllib2.Request(get_url, headers=header) response = urllib2.urlopen(req).read() txt = json.loads(response) if txt.get("paging").get("is_end"): print "爬取完毕!" break offset += 20 reg = r\u0026#39;data-actualsrc="https://www.zhihu.com/api/v4/questions/34867032/(.*?)"\u0026gt;\u0026#39; imgRe = re.compile(reg, re.S) imgUrls = imgRe.findall(str(txt)) #imgUrls = re.findall(\u0026#39;img .*?src="https://www.zhihu.com/api/v4/questions/34867032/(.*?_b.*?)"\u0026#39;, str(txt)) for imgUrl in imgUrls: try: splitPath = imgUrl.split(\u0026#39;.\u0026#39;) fTail = splitPath.pop() if len(fTail) \u0026gt; 3 : fTail = \u0026#39;jpg\u0026#39; fileName = path +"/" + str(number) + "."+fTail img_data = https://www.zhihu.com/api/v4/questions/34867032/urllib2.urlopen(imgUrl).read() output = open(fileName, /u0026#39;wb/u0026#39;) output.write(img_data) print u/u0026#39;正在保存的一张图片为/u0026#39;,fileName output.close() except urllib2.URLError as e: print e.reason number += 1 if __name__ == /u0026#39;__main__/u0026#39;: path = u/u0026#39;/home/zhenquan/pythoncode/zhihuimage/u0026#39; mkdir(path) #创建本地文件夹 downloadImage("68295490",path)
推荐阅读
- 聪明人养花,这3种“花”怎样也要养一盆,每年能省不少医药费
- 互联网怎样解决“家政服务上门速度慢”的问题
- 怎样看待从1月8号起,QQ钱包开始提现收费
- 银行it人怎样转型
- 为啥知乎上普便有一种【我在北上广深打工,所以拥有更好的视野】这样的错觉
- 汽车|冬天怎样让车内温度快速升高?座椅加热的最佳使用方式二,外循环的作用总结
- 怎样进入通信行业
- 怎样评价扶他柠檬茶的小说《云养汉》的结尾
- 怎样成为一名合格的Python程序员?
- 知乎有没有必要增加一个特别关注功能
