【发布时间】:2012-07-24 08:10:37
【问题描述】:
我是一名 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
【问题讨论】:
-
您的 PYTHONPATH 中没有名为 lxml.py 的文件?为什么第 5 行的错误和第 4 行代码示例中的导入?
-
瓦雷拉,我已经做到了。现在我得到一个“AttributeError:'module'对象没有属性'getroot'”错误......
-
你从哪里得到这个代码?有一堆错误。您需要在 indicator_xml 上调用 getroot。 indicator_XML 将是未知变量,它与 indicator_xml 不同
-
我承认我的知识有限,剪切和粘贴以及缺乏编码纪律有点被破解。请多多包涵。
标签: python screen-scraping lxml