2012-11-11 53 views
0

我使用Python 2.7.3在Windows XP 我使用Pyscripter 2.5.3.0NameError:全局名称“codepoint2name”没有定义

我试图让美丽的汤运行,使用下面的测试代码

import urllib2 
from bs4 import BeautifulSoup 

page = urllib2.urlopen("http://www.google.com") 
soup = BeautifulSoup(page) 
print soup.prettify() 

代码在dammit.py停止。错误是:

Message File Name Line Position  
Traceback    
    <module> D:\Python\TestBS.py 19  
    <module> C:\Python27\lib\site-packages\bs4\__init__.py 29  
    <module> C:\Python27\lib\site-packages\bs4\builder\__init__.py 4  
    <module> C:\Python27\lib\site-packages\bs4\element.py 5  
    <module> C:\Python27\lib\site-packages\bs4\dammit.py 49  
    EntitySubstitution C:\Python27\lib\site-packages\bs4\dammit.py 70  
    _populate_class_variables C:\Python27\lib\site-packages\bs4\dammit.py 57  
NameError: global name 'codepoint2name' is not defined 

上的代码

class EntitySubstitution(object): 

    """Substitute XML or HTML entities for the corresponding characters.""" 

    def _populate_class_variables(): 
     lookup = {} 
     reverse_lookup = {} 
     characters_for_re = [] 
     for codepoint, name in list(codepoint2name.items()): 
+0

BeautifulSoup的哪个版本? – Blender

+0

@Blender:从代码示例和追踪它似乎是版本4('bs4')。 –

回答

1

此,这部分是BeautifulSoup的错误。很明显,作者错过了从'htmlentitydefs'模块导入符号。因此,您可以通过将输入添加到 BeautifulSoup代码来自行修复此问题。另外:联系BeautifulSoup作者或者提交错误报告。

+0

如果我从htmlentitydefs导入codepoint2name print codepoint2name.items() – user1816866

相关问题