3

我一直在尝试从网站上抓取数据并将找到的数据写到文件中。超过 90% 的时间,我不会遇到 Unicode 错误,但是当数据具有以下字符(例如“汉堡王®,汉斯咖啡馆”)时,它不喜欢将其写入文件,因此我的错误处理打印它按原样显示在屏幕上,没有任何进一步的错误。

我已经尝试过编码和解码功能以及各种编码,但无济于事。

请在下面找到我编写的当前代码的摘录:

import urllib2,sys
import re
import os
import urllib
import string
import time
from BeautifulSoup import BeautifulSoup,NavigableString, SoupStrainer
from string import maketrans
import codecs

f=codecs.open('alldetails7.txt', mode='w', encoding='utf-8', errors='replace')
...


soup5 = BeautifulSoup(html5)
enc_s5 = soup5.originalEncoding

for company in iter(soup5.findAll(height="20px")):
    stream = ""
    count_detail = 1
    for tag in iter(company.findAll('td')):
        if count_detail > 1:
           stream = stream + tag.text.replace(u',',u';')
           if count_detail < 4 :
              stream=stream+","
        count_detail = count_detail + 1
    stream.strip()
    try:
        f.write(str(stnum)+","+br_name_addr+","+stream.decode(enc_s5)+os.linesep)
    except:
        print "Unicode error ->"+str(storenum)+","+branch_name_address+","+stream
4

2 回答 2

1

你的f.write()行对我来说没有意义 -stream将是 aunicode因为它是间接从tag.textBeautifulSoup 给你的 Unicode,所以你不应该打电话decodestream(您使用decodestr具有特定字符编码的 a 转换为unicode。)您已经打开了用于写入的文件codecs.open()并告诉它使用 UTF-8,因此您可以只write()使用 aunicode并且应该可以工作。所以,我会尝试:

f.write(unicode(stnum)+br_name_addr+u","+stream+os.linesep)

...或者,假设您刚刚使用 打开了文件f=open('alldetails7.txt','w'),您将执行以下操作:

line = unicode(stnum)+br_name_addr+u","+stream+os.linesep
f.write(line.encode('utf-8'))
于 2011-02-25T10:07:06.073 回答
0

您是否检查了您正在写入的文件的编码,并确保字符可以显示在您尝试写入文件的编码中?尝试将字符编码设置为 UTF-8 或其他明确的字符以显示字符。

于 2011-02-25T09:47:58.797 回答