【问题标题】:Python 3: parse html with XPath errorPython 3:使用 XPath 错误解析 html
【发布时间】:2016-06-06 03:08:49
【问题描述】:

我是 python 3 的新手。我正在将 html 数据与 XPath 配对。我使用pycharm编译我的代码,我的代码如下所示。请帮我解决这个问题(请不要使用美丽的汤)。我知道很多关于使用 python 2 xpath 解析 html 的代码,如果你有一些关于使用 python 3 xpath 解析 html 的材料链接,请告诉我。我已经在 pycharm 中安装了 lxml 和 requests 库。此外,终端默认是python 2.7。提前致谢!

  from lxml import html
  import requests

  page = requests.get('http://econpy.pythonanywhere.com/ex/001.html')
  tree = html.fromstring(page.content)

  #This will create a list of buyers:
  buyers = tree.xpath('//div[@title="buyer-name"]/text()')
  #This will create a list of prices
  prices = tree.xpath('//span[@class="item-price"]/text()')

  print('Buyers: ', buyers)
  print('Prices: ', prices)

错误:

/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5     
/Users/tianke0711/PycharmProjects/database/Pax_html/xpath_test.py

Traceback (most recent call last):
 File     
 "/Users/tianke0711/PycharmProjects/database/Pax_html/xpath_test.py", 
 line 1, in <module>
from lxml import html
  File   

"/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-   
packages/lxml/html/__init__.py", line 54, in <module>
from .. import etree

导入错误:

ddlopen(/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/lxml/etree.cpython-35m-darwin.so, 2):库未加载:libxml2.2 .dylib 引用自:/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/lxml/etree.cpython-35m-darwin.so 原因:库版本不兼容:etree.cpython-35m-darwin.so需要12.0.0或更高版本,但libxml2.2.dylib提供10.0.0版本

【问题讨论】:

  • 如果您想了解有关此错误的更多信息,请告诉我!我希望你们能帮助我解决这个问题。

标签: html xpath python-3.5


【解决方案1】:

根据错误信息,libxml2.2(Library not loaded: libxml2.2.dylib)是旧版本。由于python3需要新版本的libxml。使用以下命令安装新的 libxml,然后它对我有用。

brew install libxml2
brew install libxslt
brew link libxml2 --force
brew link libxslt --force 

其实具体原因我也不知道。如果有人知道这个,请告诉我!谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 2012-07-29
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-07
    相关资源
    最近更新 更多