1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
import turtle import requests from urllib.parse import quote import re
def obtain_coordinate(target_word): """ 获取汉字的坐标 :param target_word: :return: """ url = "https://bihua.bmcx.com/web_system/bmcx_com_www/system/file/bihua/get_0/"
params = { 'font': quote(target_word).replace("%", "").lower(), 'shi_fou_zi_dong': '1', 'cache_sjs1': '20031914', } response = requests.get(url, params=params) content = response.text content = content.replace('hzbh.main(', '').split(');document.getElementById')[0] content = content.split('{')[-1].split("}")[0] pattern = re.compile(r'\w:\[(.+?)\]') result = re.split(pattern, content) order_xy_routine = [] words_cnt = 0 for r in result: sec = re.findall(r'\'.+?\'', r) if len(sec): orders = sec[1].split('#') for order in orders: order_str = re.findall(r'\(\d+,\d+\)', order) order_xy = [eval(xy) for xy in order_str] order_xy_routine.append(order_xy) words_cnt += 1 print(order_xy_routine) return order_xy_routine
def draw_words(target_words, startx, starty, lineNum=1): """ 画汉字 :param target_words: :param startx: :param starty: :param lineNum: :return: """ turtle.color("black", "black") turtle.pu() coordinates = obtain_coordinate(target_words) for index, coordinate in enumerate(coordinates): turtle.goto((startx + coordinate[0][0])/2, -(starty + coordinate[0][1])/2) turtle.pd() for xy in coordinate: x,y=xy turtle.goto((startx+x)/2, -(starty+y)/2) turtle.pu()
if __name__ == '__main__': turtle.screensize(2400,1300) word = "教师节快乐" draw_words(word[0], -1600, 0) draw_words(word[1], -800, 0) draw_words(word[2], 0, 0) draw_words(word[3], 800, 0) draw_words(word[4], 1600, 0)
turtle.done()
|