我搜索了又搜索,但我只找到了涉及 php 而不是 python/django 的解决方案。我的目标是创建一个允许用户输入字符串的网站(后端用 python 编码)。然后,后端脚本将运行并输出带有一些信息的字典。我想要的是使用字典中的信息将其绘制到服务器上的图像上并将新图像提供给用户。我现在如何离线执行此操作?我可以使用哪些库?任何关于我应该走的路线的建议都会很好。
我还是个新手,所以如果我的代码需要改进,请原谅我。到目前为止,我所拥有的没有任何错误,但就像我说的,我不知道下一步该去哪里实现我的目标。任何提示将不胜感激。
这就是我想要的最终目标http://combatarmshq.com/dynamic-signatures.html
这就是我到目前为止所拥有的(我在这里使用了 beautiful soup 作为解析器。如果这太过分了或者我做得不太好,请告诉我是否有更好的选择。谢谢):
我获取所需数字的网址(这些是动态的)是这样的:http://combatarms.nexon.net/ClansRankings/PlayerProfile.aspx?user=
玩家的名称将位于用户之后,因此示例为 http://combatarms.nexon.net/ClansRankings/PlayerProfile.aspx?user=-aonbyte
这是具有抓取网站基本功能的代码:
from urllib import urlopen
from BeautifulSoup import BeautifulSoup
def get_avatar(player_name):
'''Return the players avatar as a binary string.'''
player_name = str(player_name)
url = 'http://combat.nexon.net/Avatar/MyAvatar.srf?'
url += 'GameName=CombatArms&CharacterID=' + player_name
sock = urlopen(url)
data = sock.read()
sock.close()
return data
def save_avatar(data, file_name):
'''Saves the avatar data from get_avatar() in png format.'''
local_file = open(file_name + '.png', 'w' + 'b')
local_file.write(data)
local_file.close()
def get_basic_info(player_name):
'''Returns basic player statistics as a dictionary'''
url = 'http://combatarms.nexon.net/ClansRankings'
url += '/PlayerProfile.aspx?user=' + player_name
sock = urlopen(url)
html_raw = sock.read()
sock.close()
html_original_parse = BeautifulSoup(''.join(html_raw))
player_info = html_original_parse.find('div', 'info').find('ul')
basic_info_list = range(6)
for i in basic_info_list:
basic_info_list[i] = str(player_info('li', limit = 7)[i+1].contents[1])
basic_info = dict(date = basic_info_list[0], rank = basic_info_list[1], kdr = basic_info_list[2], exp = basic_info_list[3], gp_earned = basic_info_list[4], gp_current = basic_info_list[5])
return basic_info
这是测试这些功能的代码:
from grabber import get_avatar, save_avatar, get_basic_info
player = raw_input('Player name: ')
print 'Downloading avatar...'
avatar_data = get_avatar(player)
file_name = raw_input('Save as? ')
print 'Saving avatar as ' + file_name + '.png...'
save_avatar(avatar_data, file_name)
print 'Retrieving ' + player + '\'s basic character info...'
player_info = get_basic_info(player)
print ''
print ''
print 'Info for character named ' + player + ':'
print 'Character creation date: ' + player_info['date']
print 'Rank: ' + player_info['rank']
print 'Experience: ' + player_info['exp']
print 'KDR: ' + player_info['kdr']
print 'Current GP: ' + player_info['gp_current']
print ''
raw_input('Press enter to close...')