0

我有以下代码,它从我的 Neo4j 数据库中获取域列表,对 IP 执行查找,然后在不存在的情况下创建关系。在创建关系的最后几行代码之前,它都可以正常工作。我收到以下错误。我已经确认这些列表有两个项目 - 域和 IP,所以我不确定它为什么会产生错误:

  File "C:\Python26\beta7_whois4j_monitor_debug.py", line 63, in createrels
  rels1 = graph_db.get_or_create_relationships((whoisnodes[0], "links", whoisnodes[1]))
  IndexError: list index out of range

这是代码:

whoisindex = graph_db.get_or_create_index(neo4j.Node, "whoisID")
domains = whoisindex.query("whoisID:*com")

for i in domains:
    list1 = []
    value1 = "{0}".format(i['whoisID'])
    try:
        e = socket.gethostbyname(value1)
    except socket.gaierror:
        e = 'exclude from list'
    if e != 'exclude from list':
        list1.append(value1)
        list1.append(e)
        for word in list1:
            whoisnodes = []
            whoisnodes.append(whoisindex.get_or_create("whoisID", word, "whoisID":word}))
            rels1 = graph_db.get_or_create_relationships(
            (whoisnodes[0], "links", whoisnodes[1]))
            print "{0}".format(i['whoisID']) 
4

2 回答 2

0

I'm a little confused as to what you are trying to do here. For each iteration of your loop for word in list you are resetting whoisnodes to a new list before adding a single item to it on the line below. This means that there can only ever be one item in the list by the time you call get_or_create_relationships, hence the IndexError when you try to access whoisnodes[1].

Did you mean for whoisnodes = [] to be outside the loop?

Incidentally, there is also a typo (missing curly bracket):

whoisindex.get_or_create("whoisID", word, "whoisID":word})

should read:

whoisindex.get_or_create("whoisID", word, {"whoisID":word})
于 2013-01-25T07:02:14.070 回答
0

我的第二次尝试虽然现在它返回一个 JSON 错误:

whoisindex = graph_db.get_or_create_index(neo4j.Node, "whoisID")
domains = whoisindex.query("whoisID:*com")

for i in domains:
list1 = []
value1 = "{0}".format(i['whoisID'])    
try:
    e = socket.gethostbyname(value1)
except socket.gaierror:
    e = 'exclude from list'
if e != 'exclude from list':
    list1.append(value1)
    list1.append(e)        
    list1.append(whoisindex.get_or_create("whoisID", i, {"whoisID":i}))
    rels1 = graph_db.get_or_create_relationships(
        (list1[0], "links", list1[1]))
    print "{0}".format(i['whoisID']) 
于 2013-01-25T16:42:42.630 回答