python从百度爬图片
来源:互联网 发布:程序员的技术栈 编辑:程序博客网 时间:2024/06/06 08:23
代码:
#!/usr/bin/env python# -*- coding:utf-8 -*-import osimport reimport urllibimport jsonimport socketimport urllib.requestimport urllib.parseimport urllib.error# 设置超时import timetimeout = 5socket.setdefaulttimeout(timeout)class Crawler: # 睡眠时长 __time_sleep = 0.1 __amount = 0 __start_amount = 0 __counter = 0 headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'} # 获取图片url内容等 # t 下载图片时间间隔 def __init__(self, t=0.1): self.time_sleep = t # 保存图片 def __save_image(self, rsp_data, word): if not os.path.exists("./" + word): os.mkdir("./" + word) # 判断名字是否重复,获取图片长度 self.__counter = len(os.listdir('./' + word)) + 1 for image_info in rsp_data['imgs']: try: time.sleep(self.time_sleep) fix = self.__get_suffix(image_info['objURL']) urllib.request.urlretrieve(image_info['objURL'], './' + word + '/' + str(self.__counter) + str(fix)) except urllib.error.HTTPError as urllib_err: print(urllib_err) continue except Exception as err: time.sleep(1) print(err) print("产生未知错误,放弃保存") continue else: print("机场+1,已有" + str(self.__counter) + "张机场") self.__counter += 1 return # 获取后缀名 @staticmethod def __get_suffix(name): m = re.search(r'\.[^\.]*$', name) if m.group(0) and len(m.group(0)) <= 5: return m.group(0) else: return '.jpeg' # 获取前缀 @staticmethod def __get_prefix(name): return name[:name.find('.')] # 开始获取 def __get_images(self, word='机场'): search = urllib.parse.quote(word) # pn int 图片数 pn = self.__start_amount while pn < self.__amount: url = 'http://image.baidu.com/search/avatarjson?tn=resultjsonavatarnew&ie=utf-8&word=' + search + '&cg=girl&pn=' + str( pn) + '&rn=60&itg=0&z=0&fr=&width=&height=&lm=-1&ic=0&s=0&st=-1&gsm=1e0000001e' # 设置header防ban try: time.sleep(self.time_sleep) req = urllib.request.Request(url=url, headers=self.headers) page = urllib.request.urlopen(req) rsp = page.read().decode('unicode_escape') except UnicodeDecodeError as e: print(e) print('-----UnicodeDecodeErrorurl:', url) except urllib.error.URLError as e: print(e) print("-----urlErrorurl:", url) except socket.timeout as e: print(e) print("-----socket timout:", url) else: # 解析json rsp_data = json.loads(rsp) self.__save_image(rsp_data, word) # 读取下一页 print("下载下一页") pn += 60 finally: page.close() print("下载任务结束") return def start(self, word, spider_page_num=1, start_page=1): """ 爬虫入口 :param word: 抓取的关键词 :param spider_page_num: 需要抓取数据页数 总抓取图片数量为 页数x60 :param start_page:起始页数 :return: """ self.__start_amount = (start_page - 1) * 60 self.__amount = spider_page_num * 60 + self.__start_amount self.__get_images(word)if __name__ == '__main__': crawler = Crawler(0.05) crawler.start('机场',2)#更改爬的图片关键词 # crawler.start('帅哥', 5)
阅读全文
0 0
- python从百度爬图片
- Python 爬取百度图片
- python爬取百度图片
- Python爬取百度图片
- python爬取百度图片
- Python 爬取百度图片
- Python爬取百度图片
- Python 爬百度帖吧图片
- python爬取百度贴吧图片
- python爬虫之爬百度美女图片
- Python爬虫爬取百度图片
- python +requests 实现爬取百度图片
- python:爬取百度贴吧图片
- 使用python爬取百度图片
- python 3 爬取百度图片
- python 爬虫爬取百度图片
- python抓取百度图片
- python 下载百度图片
- eclipse编译openwrt应用程序报Type undefined reference to `pthread_create'错误解决方法
- gradle
- oracle使用记录
- webgl学习第二集
- [JSP]Servlet之获取初始化参数inti/MVC模型
- python从百度爬图片
- Tarjan算法解强连通分量
- 7.7.1用穷举法计算两个正整数的最大公约数
- 利用休息时间编写的关于新唐单片机较为简单的ADC+定时器+IO端口程序
- golang调用lua脚本
- idea 误删文件恢复
- Introduction to Linear Algebra 学习笔记(一)
- Toj 3777 Function Problem
- Java培训总结-第1周