Python2--糗百图片下载

本文介绍如何使用Python的requests和BeautifulSoup库爬取并下载糗百用户的头像。通过获取图片URL,将头像保存到本地。
摘要由CSDN通过智能技术生成

在实现内容的爬取后,我想实现用户头像的下载。其实只需要在前一部分的代码中再加上关于图片url的链接即可。
第一个实例

  • 图片url获取
def get_data(html ):
    final = []
    pictures = []存放图片url
    bs = BeautifulSoup(html, "html.parser")
    body = bs.body
    content_left = body.find(id = 'content-left') #找到该页总框
    contents = content_left.find_all('div',class_ = 'article block untagged mb15')#找到所有内容框

    for content in contents: #对每个故事进行遍历
        temp = []
        author = content.find('div',class_='author clearfix')
        picture = author.find('img')#找到img标签
        picture_src = picture.get('src')#获取图片url
        pictures.append(picture_src)#添加到图片list中
        user_name = content.find("h2").string
        temp.append(user_name)
        data = content.find(class_ = 'content')
        story = data.find('span').get_text()
        temp.append(story)
        numbers = content.find_all('i', class_ = 'number')
        good = numbers[0].string + '好笑'
        temp.append(good)
        comment = numbers[1].string + '评论'
        temp.append(comment)
        temp.append(picture_src)
        final.append(temp)

    return final,pictures#返回data和pictures的list

这里返回了一个含有所有头像url的list,然后根据这些url将图片下载到本地。

  • 头像下载
def download_pic(pictures):
    count = 1#picture命名
    if not os.path.exists('pic'):#如果不存在pic文件夹,则创建一个名为pic的文件夹
        os.makedirs('pic')
    for picture in pictures:#对list中的url进行循环下载
        if picture == '/static/images/thumb/anony.png?v=b61e7f5162d14b7c0d5f419cd6649c87':#如果头像是默认头像则跳过
            print("静态图片")
            continue
        else:
            try:
                r = requests.get(picture)#获取头像
            except BaseException as e:
                print("图片下载失败", e)
                time.sleep(random.choice(range(30, 80)))
            else:
                filename = str(count) + '.jpg'#给头像命名
                path = "pic/" + filename
                f = open(path, 'wb')#保存头像
                f.write(r.content)
                print(count)
                count = count + 1
  • 主函数
if __name__ == '__main__':
    url ='http://www.qiushibaike.com/'
    result,picture = get_data(url)
    write_data(result, 'qiubai.csv')
    download_pic(picture)
  • 结果
    这里写图片描述
    最后可以得到用户头像和想获取的信息。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值