Python练习册(十三)——爬贴吧图片

problem0013爬贴吧图片

第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-)

  • requests发请求获取html
  • re正则表达式找到《src》里的图片地址
  • urllib.requeset.urlretrieve下载图片

demo:

#!/bin/python3

import re
import urllib
import requests


def get_html():
    response = requests.get("http://tieba.baidu.com/p/2166231880")
    new_f = open("te.html",'w')
    new_f.write(response.text)
    new_f.close()
    #print(response.text)
    return response.text

def down_png(picHtml):    
    image = re.compile(r'src="(.+?\.jpg)" pic_ext')
    imagelist = re.findall(image,picHtml)
    x = 0
    for imgurl in imagelist:
        urllib.request.urlretrieve(imgurl,'%s.jpg' % x)
        x+=1  

if __name__=='__main__':
    down_png(get_html())

参考:
python2x与3x下urlretrieve的使用
Python爬虫爬取网页图片

效果:

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_30650153/article/details/80879790