【问题标题】:Problems using etree in Python scraper在 Python 刮板中使用 etree 的问题
【发布时间】: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


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-02
    • 2023-04-02
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多