0

我是 Python 的新手,希望在 Scraperwiki 中构建屏幕抓取工具,但我正在努力解决一个我无法解决的错误。本质上,我想解析一个 xml 文件,但不知道如何让我的 gp_indicators_scrape 函数访问 getroot() 方法。

任何人都可以解决它,更重要的是,指出我的解释,以便我将来避免这个问题?

这是刮板:https ://scraperwiki.com/scrapers/choiceshu1

代码的关键位:

import lxml.html
import urlparse
from urlparse import urlparse
from lxml.etree import etree

def gp_indicators_scrape(org_URL):

     indicator_xml = etree.parse(org_URL)
     root = lxml.etree.getroot(indicator_XML)
     print root 

html = scraperwiki.scrape(combined_URL_for_first_scrape)
print html
root = lxml.html.fromstring(html)
links = root.cssselect("dd a")

这是运行时的错误

Line 5 - from lxml.etree import etree
ImportError: cannot import name etree
4

1 回答 1

1

from lxml.etree import etree应该from lxml import etree

另外,刚刚注意到 -如果您使用上面的导入,lxml.etree.getroot(...)您可以删除,通常您调用通过(或类似)返回的对象。lxml.getroot()etree.parse

注意:我没有查看提供的链接中的代码......

于 2012-07-24T08:27:38.853 回答