我使用了铁蟒的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不在“执行(范围)”的必要范围内
我不知道913是什么..
我该如何解决? Python代码正在抓取代码..它运行良好。
我有同样的问题,我搜索了我最初设置的包导入的其他包,我发现它在HTML python包中。它在库html中,文件是entities.py。该代码使两个字符列表成为代码点。然后它创建一个带有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)
我不知道你是否真的可以称之为解决方案,但我不知道如何更好地解决它。我想知道为什么这样做......