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

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

你可能感兴趣的:(Python网络编程)