我收到此错误:
UnicodeEncodeError:'ascii' 编解码器无法在位置 1409 编码字符 u'\u2661':序数不在范围内(128)
我对编程仍然很陌生,所以请怜悯我和我的无知。但我理解错误是它无法处理 unicode 字符。至少有一个 unicode 字符,但可能还有无数其他字符会在该提要中振作起来。
我已经做了一些寻找有类似问题的其他人,但我找不到我理解或可以工作的解决方案。
#import library to do http requests:
import urllib
from xml.dom.minidom import parseString, parse
f = open('games.html', 'w')
document = urllib.urlopen('https://itunes.apple.com/us/rss/topfreemacapps/limit=300/genre=12006/xml')
dom = parse(document)
image = dom.getElementsByTagName('im:image')
title = dom.getElementsByTagName('title')
price = dom.getElementsByTagName('im:price')
address = dom.getElementsByTagName('id')
imglist = []
titlist = []
pricelist = []
addlist = []
i = 0
j = 20
k = 40
f.write('''\
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
<!--
A:link {text-decoration: none; color: #246DA8;}
A:visited {text-decoration: none; color: #246DA8;}
A:active {text-decoration: none; color: #40A9E3;}
A:hover {text-decoration: none; color: #40A9E3;}
.box {
vertical-align:middle;
width: 180px;
height: 120px;
border: 1px solid #99c;
padding: 5px;
margin: 0px;
margin-left: auto;
margin-right: auto;
-moz-border-radius: 5px;
border-radius: 5px;
-webkit-border-radius: 5px;
background-color:#ffffff;
font-family: Arial, Helvetica, sans-serif; color: black;
font-size: small;
font-weight: bold;
}
-->
</style>
</head>
<body>
''')
for i in range(0,len(image)):
if image[i].getAttribute('height') == '53':
imglist.append(image[i].firstChild.nodeValue)
for i in range(1,len(title)):
titlist.append(title[i].firstChild.nodeValue)
for i in range(0,len(price)):
pricelist.append(price[i].firstChild.nodeValue)
for i in range(1,len(address)):
addlist.append(address[i].firstChild.nodeValue)
for i in range(0,20):
f.write('''
<div style="width: 600px;">
<div style="float: left; width: 200px;">
<div class="box" align="center">
<div align="center">
<a href="''' + addlist[i] + '''?at=10l5NR" target="_blank">''' + titlist[i] + '''</a><br>
<a href="''' + addlist[i] + '''?at=10l5NR" target="_blank"><img src="''' + imglist[i] + '''" alt="" width="53" height="53" border="0" ></a><br>
<span>''' + pricelist[i] + '''</span>
</div>
</div>
</div>
<div style="float: left; width: 200px;">
<div class="box" align="center">
<div align="center">
<a href="''' + addlist[i+j] + '''?at=10l5NR" target="_blank">''' + titlist[i+j] + '''</a><br>
<a href="''' + addlist[i+j] + '''?at=10l5NR" target="_blank"><img src="''' + imglist[i+j] + '''" alt="" width="53" height="53" border="0" ></a><br>
<span>''' + pricelist[i+j] + '''</span>
</div>
</div>
</div>
<div style="float: left; width: 200px;">
<div class="box" align="center">
<div align="center">
<a href="''' + addlist[i+k] + '''?at=10l5NR" target="_blank">''' + titlist[i+k] + '''</a><br>
<a href="''' + addlist[i+k] + '''?at=10l5NR" target="_blank"><img src="''' + imglist[i+k] + '''" alt="" width="53" height="53" border="0" ></a><br>
<span>''' + pricelist[i+k] + '''</span>
</div>
</div>
</div>
<br style="clear: left;" />
</div>
<br>
''')
f.write('''</body>''')
f.close()