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

来源:互联网 发布:中国吸血鬼网络剧 编辑:程序博客网 时间:2024/05/18 03:57

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

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

本次抓取Google图片

# getimage.pyimport urllib.requestimport refrom urllib.error import HTTPError, URLErrorurl = '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 browserheaders = {'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 urlfb = urllib.request.urlopen(url2)souCode = fb.read().decode('utf-8')# get what you want form souCodedownLists = re.findall('http\S+.jpg', souCode)# download form urlLists to your catalogi = 0for 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 0
原创粉丝点击