首页 文章

Python 3:用XPath错误解析html

提问于
浏览
0

我是python 3的新手 . 我正在使用XPath削减html数据 . 我使用pycharm编译我的代码,我的代码如下所示 . 请帮我修复问题(请不要使用美丽的汤) . 我知道很多关于用python 2 xpath解析html的代码,如果你有一些关于用python 3 xpath解析html的材料链接,请告诉我 . 我在pycharm中安装了lxml和request库 . 此外,终端默认是python 2.7.Thanks提前!

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 . 因此需要12.0.0或更高版本,但libxml2.2.dylib提供版本10.0.0

1 回答

  • 0

    根据错误信息,libxml2.2(未加载库:libxml2.2.dylib)是旧版本 . 由于python3需要新版本的libxml . 使用以下命令安装新的libxml,然后它适用于我 .

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

    实际上,我不清楚原因 . 如果有些人知道这一点,请告诉我!谢谢!

相关问题