python网络爬虫(1)--抓取图片(2)

上一篇用来HTML解析器来解析网页源代码,这次用正则表达式来解析

同上一篇略同,代码如下:

本次抓取Google图片

# getimage.py
import urllib.request
import re
from urllib.error import HTTPError, URLError


url = 'https://www.google.com.hk/search?safe=strict&hl=zh-CN&biw=1366&bih=638&s' \
      'ite=imghp&tbm=isch&sa=1&btnG=Google+%E6%90%9C%E7%B4%A2&q=%E8%87%AA%E7%84%B6'
# pretend as a browser
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1;\
             WOW64; rv:23.0) Gecko/20100101 Firefox/23.0 '}
url2 = urllib.request.Request(url, headers=headers)

# get the source code form url
fb = urllib.request.urlopen(url2)
souCode = fb.read().decode('utf-8')

# get what you want form souCode
downLists = re.findall('http\S+.jpg', souCode)

# download form urlLists to your catalog
i = 0
for lists in downLists:
    print(lists)
    try:
        urllib.request.urlretrieve(lists, 'C:\\image\\nature\\nature%s.png' % i)
    except HTTPError:
        continue
    except URLError:
        continue
    except UnicodeEncodeError:
        continue
    i += 1

注意:需要异常处理,以为有些图片是打不开的,或网页编程者出错的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值