执行期间Python错误

时间:2014-01-19 15:04:44

标签: python beautifulsoup

我正在尝试使用漂亮的汤来获取网站的数据,它的运行非常适合前4-5个查询但在此之后我收到此错误:

Description:  Traceback (most recent call last):
File "./r.py", line 11, in <module>
print "Description: ",k.text
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 16: ordinal not in range(128)

这是我的代码:

#!/usr/bin/python
from bs4 import BeautifulSoup
import urllib2
response = urllib2.urlopen('http://websitename.com')
html = response.read()
soup = BeautifulSoup(html)
for i in  soup.findAll("div", {"class": "ParentDiv"}):
        for j in  i.findAll("a",limit=1):
                print "Name is: ",j.text
        for k in i.findAll("div", {"class":"description"},limit=1):
                print "Description: ",k.text
        for l in i.findAll("div", {"class":"Information"},limit=1):
                print "Addition info",l.text

1 个答案:

答案 0 :(得分:0)

解码html变量中的某些内容,因为当前BF认为它是ascii,而它显然不是。

如果您不需要使用urllib,请尝试requests,通常他们可以为您取消数据。