Python:爬取网站图片并保存至本地

Python:爬取网图片并保存至本地

python3爬取网页中的图片到本地的过程如下:

1爬取网页

2、获取图片地址

3爬取图片内容并保存到本地

 

 

实例:爬取百度贴吧首页图片。

 

代码如下:

import urllib.request

import re


url = 'https://tieba.baidu.com/'

request = urllib.request.Request(url)

request.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360EE')

response = urllib.request.urlopen(request)

buf = response.read()

buf = str(buf, encoding='utf-8')

# 获取所有图片url地址列表

listurl = re.findall(r'http.+\.jpg', buf)

print(listurl)


i = 1

for url in listurl:

    with open(str(i)+'.jpg','wb+') as f:

        req = urllib.request.urlopen(url)

        buf = req.read()

        f.write(buf)

        i += 1

 

参考资料:

https://blog.csdn.net/qq_37811638/article/details/84586200

你可能感兴趣的:(Python:爬取网站图片并保存至本地)