1. 程式人生 > >爬取網易熱評做成網易雲熱門截圖形式(給自己看的很亂有待更改+)

爬取網易熱評做成網易雲熱門截圖形式(給自己看的很亂有待更改+)

json roman reply 熱門 req fan sta strftime ram



# 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/msyh.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
= rC:\Users\HDWEN\Desktop\test\test1 path = rC:\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 + 50), %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 + 50), %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),‘??‘, font=ImageFont.truetype(webding_font, 150), fill=‘black‘) # draw.text((100, height - 450), ‘Hdwen‘, font=ImageFont.truetype(english_font, 250), fill=‘black‘) # 回復區 image.save(os.path.join(path, os.path.basename(url)), jpeg) img3 = Image.open(os.path.join(path, os.path.basename(url))) img3.paste(img1, (50, 50)) if reply_user != ‘‘: img = ImageDraw.Draw(img3) # 若是分行的話就要參照評論第二行的高度了 h = max(all_h) + 3 text2 = reply_user + + reply_content def multi_line(text, h): if width - 1400 > ft.getsize(text)[0]: img.rectangle((1300, h * 250 + 20, width, (line) * 250), fill=(250, 240, 230)) img.text((1400, h * 250 + 50), %s % text, fill=black, font=ft) for i in range(1, len(text) + 1): if width - 1400 - 250 < ft.getsize(text[:i])[0] < width - 1400: img.rectangle((1300, h * 250-150 , width, (line ) * 250+150), fill=(250, 240, 230)) img.text((1400, h * 250 + 50), %s % (text[:i]), fill=black, font=ImageFont.truetype(fangsong_font, 200)) h += 1 t = text[i:] all_h.append(h) multi_line(t, h) multi_line(text2, h) # img.text((1400,650),‘%s:‘%(reply_user),font=ImageFont.truetype(nor_font, 250),fill=‘blue‘) # img.text((1400+ft.getsize(reply_user)[0]+ 250,650),‘%s‘%(reply_content),font=ImageFont.truetype(fangsong_font, 250),fill=‘black‘) # print(len(text),one_line_len) img3.show() img3.save(os.path.join(rC:\Users\HDWEN\Desktop\song_pic, os.path.basename(url))) params = N/k4O/N6NXalQC6Rv9BW8PTzWfT5CNZadhKUGlbtvyv4Txkq6VA4hy9CsYGAukRmtMJ2fhEF0IuVTxYdrhmpkb6WkYO25h/RV0uDd6dC9W7wU8y1Jt3+HlIPnnqvZeEgUOM8DcUZKx6Br+YcWI6G3v7ZPSecMA90sWdNXb9DEZhtcZD+V2GCRQxp/vxFgepdy/KaT1P8mMZ4wNdW99PYWMVLNprGNuyc8/GgMnIYHWQ= encSecKey = a1264cb1d89ebc410d9a6d7ebae75fd78798c66e17b05299fe7564b33edda38653454ee8ed240c1eb77b8d159478ff6d9cd3521943371a1dd682474a1218c30800090541d87f6cbde133c69158a4bf72141c48872ecd3248578079cc1be6e2fb6b0dc80749857d893eae0ab4f516794a11cc8e39210659e421a4a6f7a8f5cf4d headers = { Cookie: appver=1.5.0.75771;, Referer: http://music.163.com/, User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36 } data = { "params": params, "encSecKey": encSecKey } song_id = 35090300 url = http://music.163.com/weapi/v1/resource/comments/R_SO_4_{}?csrf_token=63c8e79f67b0ee78ce7f3d38a5081b2e.format( song_id) # format裏面填寫的是歌曲的id url1=http://music.163.com/song?id={}.format(song_id) res = requests.post(url, headers=headers, data=data) dict = json.loads(res.content) print(dict) dicts = dict[hotComments] print(dicts) res1=requests.get(url1).text a=re.findall(<title>(.*?)</title>,res1) song_name=a[0][:-12].split(-) print(song_name:,song_name[0]) print(singer:,song_name[1]) for i in dicts: user = i[user][nickname] comment = ‘‘.join([t for t in i[content] if t not in {\n, }]) count = i[likedCount] url = i[user][avatarUrl] time_stamp = int(str(i[time])[:-3]) reply = i[beReplied] print(i[time]) if reply == []: # pass get_pic(user, url, str(comment), count, time_stamp) print(i[user][nickname], :, comment, *****Count:, i[likedCount], i[user][avatarUrl], int(str(i[time])[:-3])) else: reply_user = i[beReplied][0][user][nickname] reply_content = ‘‘.join([t for t in i[beReplied][0][content] if t not in {\n, }]) get_pic(user, url, comment, count, time_stamp, reply_user, reply_content) # print(i[‘user‘][‘nickname‘], ‘:‘, i[‘content‘], ‘對於這個評論的回復:‘, i[‘beReplied‘][0][‘content‘], ‘Count:‘, # i[‘likedCount‘]) # print(list(i[‘beReplied‘][0][‘content‘])) # print(list(reply_content))

 

技術分享技術分享

爬取網易熱評做成網易雲熱門截圖形式(給自己看的很亂有待更改+)