【发布时间】:2014-09-23 10:18:43
【问题描述】:
1.我使用的是 Python 2.6.6
2.下载BeautifulSoaup和lxml外部解析器,解压并解压bs4和lxml文件夹。
3.编写link.py如下
#! /usr/bin/python
from bs4 import BeautifulSoup
from urllib import urlopen
from lxml import etree
webpage = urlopen('http://www.google.com')
soup=BeautifulSoup(webpage, "lxml")
for link in soup.find_all('a'):
print(link.get('href'))
运行 ./link.py 时出现以下错误:
Traceback (most recent call last):
File "./link.py", line 5, in <module>
from lxml import etree
ImportError: cannot import name etree
【问题讨论】:
-
pip install lxml==3.1.2 -
lxml 3.4 坏了吗?我也有同样的问题 !为python 2和3都安装了lxml,但我不能
from lxml import etree -
这可能的原因:您运行的文件名为“lxml.py”,因此无法从导入的同一位置导入 etree。附言这不是你的情况,但可能是有人在这里用谷歌搜索的原因:将你的脚本名称从 lxml.py 更改为其他名称!