简体   繁体   English

执行期间发生Python错误

[英]Python Error during execution

I'm trying to fetch website's data using beautiful soup, its running perfect for first 4-5 queries but after that I get this error: 我正在尝试使用漂亮的汤来获取网站的数据,该网站对于前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)

Here is my code: 这是我的代码:

#!/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

Decode something that is in your html variable, because at the moment BF considers it ascii, and it apparently isn't. 解码html变量中的内容,因为目前BF认为它是ascii,但显然不是。

Decodings are pain in the back - if you dont have to use urllib, try requests , usually they are able to deocde data for you. 解码在后面很麻烦-如果您不必使用urllib,请尝试request ,通常它们可以为您解码数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM