爬取网易热评做成网易云热门截图形式(给自己看的很乱有待更改+)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了爬取网易热评做成网易云热门截图形式(给自己看的很乱有待更改+)相关的知识,希望对你有一定的参考价值。

# coding: utf-8



import requests, json, os, time, math, re
from PIL import Image, ImageDraw, ImageFont



# 字体区
nor_font = \'C:/windows/fonts/simsun.ttc\'
micro_font = \'C:/windows/fonts/msyhbd.ttc\'  # 微软雅黑
times_font = \'C:/windows/fonts/times.ttf\'  # times new Roman
huawen_font = \'C:/windows/fonts/STXINGKA.TTF\'  # 华文行楷
english_font = \'C:/windows/fonts/STXINGKA.TTF\'  # 英文的
fangsong_font = \'C:/windows/fonts/simfang.ttf\'  # 仿宋
consolab_font = \'C:/windows/fonts/consolab.ttf\'
webding_font = \'C:/windows/fonts/webdings.ttf\'
itckrist_font = \'C:/windows/fonts/ITCKRIST.TTF\'
simyou_font = \'C:/windows/fonts/SIMYOU.TTF\'


path_req_img = r\'C:\\Users\\HDWEN\\Desktop\\test\\test1\'
path = r\'C:\\Users\\HDWEN\\Desktop\\test\'



def get_pic(user, url, comment, count, time_stamp, reply_user=None, reply_content=None):
    # 将头像图片都设置为固定大小(1024*1024)
    with open(os.path.join(path_req_img, os.path.basename(url)), \'wb\') as f:
        f.write(requests.get(url).content)
    img1 = Image.open(os.path.join(path_req_img, os.path.basename(url)))
    img1 = img1.resize((1024, 1024), Image.ANTIALIAS)
    width1 = img1.size[0]
    height1 = img1.size[1]
    img1 = img1.crop((0, 0, width1, height1))
    # print(width1,height1)
    # 计算文字需要的行数

    if reply_user == None: reply_content = \' \';reply_user = \'\'
    # 评论区
    # comment
    ft = ImageFont.truetype("%s" % fangsong_font, 200)
    # 一行设置为50个字
    one_zh_width = ft.getsize(\'\')[0]
    width = 1500 * 8
    w1, h1 = ft.getsize(user + comment)
    w2, h2 = ft.getsize(reply_user + reply_content)
    line_w1 = math.ceil(w1 / (width - 1100)) + 1
    line_w2 = math.ceil(w2 / (width - 1400)) + 1
    line = line_w1 + line_w2
    height = (line + 5) * 250
    # print(line_w1,line_w2,height)
    # 创建空白图片
    image = Image.new(\'RGB\', (width, height), (247, 238, 214))
    draw = ImageDraw.Draw(image)
    # 评论区

    text1 = user + \'\' + comment
    ft = ImageFont.truetype(\'C:/windows/fonts/simfang.ttf\', 200)
    all_h = []

    def multi_line(text, h=0):
        if width - 1100 >= ft.getsize(text)[0]:
            draw.text((1100, h * 250 + 100), \'%s\' % text, fill=\'black\', font=ft)
            all_h.append(0)
        else:
            for i in range(int((width - 1100) / 250), len(text) + 1):
                if width - 1100 - 250 < ft.getsize(text[:i])[0] < width - 1100:
                    draw.text((1100, h * 250 + 100), \'%s\' % (text[:i]), fill=\'black\', font=ft)
                    h += 1
                    t = text[i:]
                    all_h.append(h)
                    multi_line(t, h)

    multi_line(text1)


    # 点赞区
    draw.text((width - 2200, height - 400), \'点赞:(\' , font=ImageFont.truetype(fangsong_font, 200),
              fill=\'black\')
    draw.text((width - 2200+ft.getsize(\'点赞:(\')[0], height - 400), \'%s\' % (count), font=ImageFont.truetype(times_font, 200),
              fill=\'gray\')
    draw.text((width - 2200+ft.getsize(\'点赞:(\')[0]+ft.getsize(str(count))[0], height - 400), ")|回复", font=ImageFont.truetype(fangsong_font, 200),
              fill=\'black\')

    # 时间区
    shift_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time_stamp))
    draw.text((width - 5000, height - 400), \'%s\' % shift_time, font=ImageFont.truetype(fangsong_font, 200), fill=\'gray\')

    # 给自己Hdwen打一个水印及歌曲名
    draw.text((150,height - 400),\'song:\',font=ImageFont.truetype(itckrist_font, 200), fill=\'gray\')
    draw.text((150+ft.getsize(\'song:\')[0]+150, height - 400),\'%s\'%song_name[0], font=ImageFont.truetype(itckrist_font, 200), fill=\'red\')

    # draw.text((1100+ft.getsize(str(shift_time))[0]+ft.getsize(str(song_name[0]))[0]+2700, height - 350),\'

以上是关于爬取网易热评做成网易云热门截图形式(给自己看的很乱有待更改+)的主要内容,如果未能解决你的问题,请参考以下文章

抓取网易云音乐歌曲热门评论生成词云

Scrapy框架实战爬取网易严选-苹果12手机热评

简单的网易云音乐热门评论爬虫

网易云音乐评论爬虫:歌曲的全部评论

如何用Python网络爬虫爬取网易云音乐歌词

如何用Python网络爬虫爬取网易云音乐歌词