补充:批量下载百度贴吧图片Demo

版权声明:本文为博主原创文章,注明出处,随意转载。 https://blog.csdn.net/IMW_MG/article/details/78594370

之前在我的一篇博客中写了一个爬虫小程序,是用于通过接收用户百度贴吧帖子网址输入,然后批量下载楼层图片的一个小爬虫【博客地址】。
实现特别简单,都是获取百度贴吧图片资源的url信息,不过这次换作用BS4模块来实现,下面写了一个小Demo

# !/usr/bin/env python

from urllib.request import urlopen
from bs4 import BeautifulSoup
from urllib.request import urlretrieve

url = input('请输入贴吧贴子URL:')
html = urlopen(url)
bsObj = BeautifulSoup(html, 'lxml')

imgLinks = bsObj.find('cc').find_all('img', {'class': 'BDE_Image'})     # 百度贴吧所有楼层图片链接所在标签,返回带标签列表

x = 0
for item in imgLinks:
    x += 1
    print('Downloading... ==> [%d]' % x)
    print(item['src'])      
    urlretrieve(item['src'], filename=str(x) + '.jpg')  # 存储命名方式

运行结果如下

请输入贴吧贴子URL:>? https://tieba.baidu.com/p/5444626189
Downloading... ==> [1]
https://imgsa.baidu.com/forum/w%3D580/sign=5e23c902083b5bb5bed720f606d2d523/f61fdcc451da81cb4fcc4b645966d016082431e6.jpg
Downloading... ==> [2]
https://imgsa.baidu.com/forum/w%3D580/sign=01ce71cd9feef01f4d1418cdd0ff99e0/dca7267f9e2f07089d79e2a9e224b899a801f289.jpg
Downloading... ==> [3]
https://imgsa.baidu.com/forum/w%3D580/sign=bc7cfed673f0f736d8fe4c093a54b382/75aca2cc7cd98d1016f1c4982a3fb80e7aec90e7.jpg

猜你喜欢

转载自blog.csdn.net/IMW_MG/article/details/78594370