Python2--糗百图片下载

来源:互联网 发布:淘宝三哥家店名叫什么 编辑:程序博客网 时间:2024/05/16 07:38

在实现内容的爬取后,我想实现用户头像的下载。其实只需要在前一部分的代码中再加上关于图片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