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)```




最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容