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

来源:互联网 发布:西安交通大学软件学院 编辑:程序博客网 时间:2024/04/18 16:51

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

# !/usr/bin/env pythonfrom urllib.request import urlopenfrom bs4 import BeautifulSoupfrom urllib.request import urlretrieveurl = input('请输入贴吧贴子URL:')html = urlopen(url)bsObj = BeautifulSoup(html, 'lxml')imgLinks = bsObj.find('cc').find_all('img', {'class': 'BDE_Image'})     # 百度贴吧所有楼层图片链接所在标签,返回带标签列表x = 0for 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/5444626189Downloading... ==> [1]https://imgsa.baidu.com/forum/w%3D580/sign=5e23c902083b5bb5bed720f606d2d523/f61fdcc451da81cb4fcc4b645966d016082431e6.jpgDownloading... ==> [2]https://imgsa.baidu.com/forum/w%3D580/sign=01ce71cd9feef01f4d1418cdd0ff99e0/dca7267f9e2f07089d79e2a9e224b899a801f289.jpgDownloading... ==> [3]https://imgsa.baidu.com/forum/w%3D580/sign=bc7cfed673f0f736d8fe4c093a54b382/75aca2cc7cd98d1016f1c4982a3fb80e7aec90e7.jpg
原创粉丝点击