国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python使用爬蟲抓取美女圖片并保存到本地的方法【測試可用】

2020-01-04 14:36:53
字體:
來源:轉載
供稿:網友

本文實例講述了Python使用爬蟲抓取美女圖片并保存到本地的方法。分享給大家供大家參考,具體如下:

圖片資源來自于www.qiubaichengren.com

代碼基于Python 3.5.2

友情提醒:血氣方剛的騷年。請

謹慎閱圖!
謹慎閱圖!!
謹慎閱圖!!!

code:

#!/usr/bin/env python# -*- coding: utf-8 -*-import osimport urllibimport urllib.requestimport refrom urllib.error import URLErrorclass QsSpider:  def __init__(self):    self.user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'    self.header = {'User-Agent': self.user_agent}    self.save_dir = './pic'    self.url = 'http://www.qiubaichengren.com/%s.html'  def start(self):    for i in range(1, 10):      self.load_html(str(i))  def load_html(self, page):    try:      web_path = self.url % page      request = urllib.request.Request(web_path, headers=self.header)      with urllib.request.urlopen(request) as f:        html_content = f.read().decode('gb2312')        # print(html_content)        self.pick_pic(html_content)    except URLError as e:      print(e.reason)    return  def save_pic(self, img):    print(img)    save_path = self.save_dir + "/" + img.replace(':', '@').replace('/', '_')    if not os.path.exists(self.save_dir):      os.makedirs(self.save_dir)    print(save_path)    urllib.request.urlretrieve(img, save_path)    pass  def pick_pic(self, html_content):    regex = r'src="(http:.*?/.(?:jpg|png|gif))'    patten = re.compile(regex)    pic_path_list = patten.findall(html_content)    for i in pic_path_list:      self.save_pic(str(i))      print(i)spider = QsSpider()spider.start()

代碼運行后可得到如下N多大飽眼福的美女圖:

Python,爬蟲,美女圖片

希望本文所述對大家Python程序設計有所幫助。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大港区| 东乡县| 白沙| 民丰县| 新宾| 灯塔市| 兴安县| 都匀市| 山阳县| 阿克陶县| 江西省| 聊城市| 玉山县| 会昌县| 靖州| 永城市| 昌图县| 古丈县| 三门峡市| 泽普县| 项城市| 泸水县| 响水县| 旬邑县| 稷山县| 平舆县| 治县。| 清水县| 曲沃县| 巴彦县| 马鞍山市| 桂平市| 宁阳县| 通城县| 宿州市| 正镶白旗| 昭苏县| 林州市| 洛浦县| 吉安市| 华容县|