用beautifulSoup +urllib2分析网页时中文链接不可输出到txt

英文链接好好的,比如 www.sss.com/keyword=english, 输出没问题

但是中文就不行了,如 www.ssss.com/keyword=中文
中文的可以 print 在运行结果里,但无法输出至 txt

程序代码:

paramFile = codecs.open('e:/Dataresult.txt', 'w', 'gb18030')
dataFile = open('E:\\new.txt') 
...
page = urllib2.Request(args, headers=headers) 
html = r.read()
soup = BeautifulSoup(html, 'lxml')
....
param = soup.title.a
print>>paramFile, product+'\r\n'

错误代码:

Traceback (most recent call last):
  File "C:/Users/ctbri-xxx/PycharmProjects/bs/alpha0.1.py", line 213, in <module>
    print>>paramFile, product+'\r\n'
  File "C:\Python27\lib\codecs.py", line 706, in write
    return self.writer.write(data)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xbb in position 0: ordinal not in range(128)
阅读 2.6k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进