python 爬虫抓取图片

最好的方法是不看语法不看文档,用什么搜什么
抓取图片

import urllib.request
import re
def dowload_page(url):
    request = urllib.request.urlopen(url)
    data = request.read()
    return data
def get_html(url):
    request = urllib.request.urlopen(url)
    data = request.read()
    hmtl =data.decode('UTF-8')
    return hmtl
def get_image(html):
     regx = r'http://[\S]*\.jpg'
     pattern = re.compile(regx)
     get_img = re.findall(pattern,repr(html))
     print(get_img)
     print('aaa')
     num =1
     for img in get_img:
         image = dowload_page(img)
         with open('%s.jpg'%num,'wb') as fp:
             fp.write(image)
             num += 1
             print('正在下载第%s张图片'%num)
     return


html =get_html('http://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gbk&word=%B7%E7%BE%B0&fr=ala&ala=1&alatpl=adress&pos=0&hs=2&xthttps=000000')
get_image(html)```




你可能感兴趣的:(python 爬虫抓取图片)