如何解决IronPython错误

时间:2018-05-30 08:13:45

标签: c# python ironpython

我使用了铁蟒的c#和python ..

这是我的代码:

var engine = IronPython.Hosting.Python.CreateEngine();
var searchPaths = engine.GetSearchPaths();
searchPaths.Add(@"C:\Program Files\IronPython 2.7\Lib");
searchPaths.Add(@"C:\Users\AppData\Local\Programs\Python\Python36-32");
searchPaths.Add(@"C:\Users\AppData\Local\Programs\Python\Python36-32\Lib");
searchPaths.Add(@"C:\Users\AppData\Local\Programs\Python\Python36-32\Lib\site-packages");
engine.SetSearchPaths(searchPaths);

String URL = "";

try
{
    URL = URLTB.Text.Trim();
    var scope = engine.CreateScope();
    engine.CreateScriptSourceFromFile("crawling.py").Execute(scope);

    var setUrl = scope.GetVariable<Func<object, object>>("setURL");
    var data = setUrl(URL);
}
catch (Exception ex)
{
    MessageBox.Show("Crawling error : " + ex.Message);
}

但我收到了错误:

  

913不在要求的范围内&#34;在&#39;执行(范围)&#39;

我不知道913是什么..

我该如何解决? Python代码正在抓取代码..它运行良好。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,我搜索了其他与最初设置的包一起导入的包,发现它位于HTML python包中。它在html库中,文件为Entitys.py。该代码列出了两个chars代码列表。然后,使用char的名称制作一个字典并设置char。像这样:

# maps the Unicode code point to the HTML entity name
codepoint2name = {}

# maps the HTML entity name to the character
# (or a character reference if the character is outside the Latin-1 range)
entitydefs = {}

for (name, codepoint) in name2codepoint.items():
    codepoint2name[codepoint] = name
    entitydefs[name] = chr(codepoint)

显然,C#IronPython在python的chr(n)中不接受大于255的值。 我通过在for中添加if来“解决”该问题:

for (name, codepoint) in name2codepoint.items():
    codepoint2name[codepoint] = name
    if(codepoint <= 255):
        entitydefs[name] = chr(codepoint)

我不知道您是否真的可以将此称为解决方案,但我不知道如何更好地解决它。我想知道为什么会这样...