第一个爬虫小程序

爬取慕课网首页图片

import re
from urllib import request

url = 'http://www.imooc.com/course/list'
r = request.urlopen(url)
str_content = r.read()
str_content = str_content.decode('utf-8')
#贪婪匹配,必须要加问号
imgs_list = re.findall(r"//img.+?\.jpg",str(str_content)) 
imgs_list = set(imgs_list)
#~ print(imgs_list)
i = 0
for img in imgs_list:
    #必须要加b,以二进制方式打开
    f = open(str(i)+'.jpg','wb')
    data = request.urlopen('http:'+img).read()
    f.write(data)
    i+=1

你可能感兴趣的:(python,爬虫)