小编Gle*_*enn的帖子

Python字符串处理,Unicode和Beautiful Soup

我一直在寻找我所拥有的但没有找到/理解一个可行的错误的解决方案.基本上,如果我使用字符串函数(translate,strip等),我会得到Unicode错误(ascii'编解码器无法在位置y编码字符'x':序数不在范围内(128).但是当我尝试美丽的汤时处理文本,我没有得到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

trantab=string.maketrans(",",";") 
...

                html5 = urllib2.urlopen(address5).read()
                time.sleep(1.5)

                soup5 = BeautifulSoup(html5)

                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 + string.translate(str(tag.text),trantab)
                            if count_detail < 4 :
                                stream=stream+","
                        count_detail = count_detail + 1
                    print str(storenum)+","+branch_name_address+","+ stream
Run Code Online (Sandbox Code Playgroud)

....

这个脚本运行一段时间,然后炸弹 stream = stream + string.translate(str(tag.text),trantab)

基本上,我只是想在我正在处理的字段中用分号替换逗号.

此外,尝试删除嵌入的空白/空白, …

python string unicode beautifulsoup

2
推荐指数
1
解决办法
2761
查看次数

标签 统计

beautifulsoup ×1

python ×1

string ×1

unicode ×1