运行Python 2.7.5和OSX 10.8。还在使用Anaconda包管理器创建的虚拟环境中运行Python。我已经安装了命令行工具XCode 4.6.3。
使用scraperwiki示例:
import scraperwiki
html = scraperwiki.scrape('https://scraperwiki.com/hello_world.html')
import lxml.cssselect
import lxml.html
root = lxml.html.fromstring(html) # turn our HTML into an lxml object
tds = root.cssselect('td') # get all the <td> tags
for td in tds:
print lxml.html.tostring(td) # the full HTML tag
print td.text # just the text inside the HTML tag
尝试使用lxml.cssselect会引发此错误:
/Users/mmoncrief/anaconda/envs/py27/lib/python2.7/site-packages/lxml/cssselect.py in <module>()
16 external_cssselect = __import__('cssselect')
17 except ImportError:
---> 18 raise ImportError('cssselect seems not to be installed. '
19 'See http://packages.python.org/cssselect/')
20
ImportError: cssselect seems not to be installed. See http://packages.python.org/cssselect/
使用IPython,我在lxml(输入lxml。)下查找已安装的模块,只查找lxml.etree和lxml.get_include:no cssselect module。
我尝试使用pip install lxml --update更新lxml。这回复:
Running setup.py egg_info for package lxml
/Users/mmoncrief/anaconda/envs/py27/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'bugtrack_url'
warnings.warn(msg)
Building lxml version 3.2.3.
Building without Cython.
Using build configuration of libxslt 1.1.28
Building against libxml2/libxslt in the following directory: /Users/mmoncrief/anaconda/envs/py27/lib
warning: no files found matching '*.txt' under directory 'src/lxml/tests'
Downloading/unpacking update
Could not find any downloads that satisfy the requirement update
No distributions at all found for update
Storing complete log in /Users/mmoncrief/.pip/pip.log
尝试卸载lxml并使用pip重新安装,我得到一个clang错误和seg错误。这是pip.log的部分:
/Users/mmoncrief/anaconda/envs/py27/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'bugtrack_url'
warnings.warn(msg)
Building lxml version 3.2.3.
Building without Cython.
Using build configuration of libxslt 1.1.28
Building against libxml2/libxslt in the following directory: /Users/mmoncrief/anaconda/envs/py27/lib
running install
running build
running build_py
copying src/lxml/includes/lxml-version.h -> build/lib.macosx-10.5-x86_64-2.7/lxml/includes
running build_ext
building 'lxml.etree' extension
/usr/bin/clang -fno-strict-aliasing -I/Users/mmoncrief/anaconda/envs/py27/include -arch x86_64 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/mmoncrief/anaconda/envs/py27/include -I/Users/mmoncrief/anaconda/envs/py27/include/libxml2 -I/private/var/folders/l2/zvz4t5rs7pb3n1bc4_bdz6n4cbzgwl/T/pip-build-mmoncrief/lxml/src/lxml/includes -I/Users/mmoncrief/anaconda/envs/py27/include/python2.7 -c src/lxml/lxml.etree.c -o build/temp.macosx-10.5-x86_64-2.7/src/lxml/lxml.etree.o -flat_namespace
clang: warning: argument unused during compilation: '-flat_namespace'
src/lxml/lxml.etree.c:136455:17: warning: enumeration value '__pyx_e_4lxml_5etree_PARSER_DATA_INVALID' not handled in switch [-Wswitch]
switch (__pyx_v_doc_ref->_type) {
^
src/lxml/lxml.etree.c:140541:72: warning: incompatible pointer types passing 'struct __pyx_obj_4lxml_5etree__BaseContext *' to parameter of type 'struct __pyx_obj_4lxml_5etree__XSLTContext *' [-Wincompatible-pointer-types]
__pyx_t_1 = ((PyObject *)__pyx_f_4lxml_5etree_12_XSLTContext__copy(((struct __pyx_obj_4lxml_5etree__BaseContext *)__pyx_v_self->_context))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 543; __pyx_clineno = __LINE__; goto __pyx_L9;}
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/lxml/lxml.etree.c:138476:138: note: passing argument to parameter '__pyx_v_self' here
static struct __pyx_obj_4lxml_5etree__BaseContext *__pyx_f_4lxml_5etree_12_XSLTContext__copy(struct __pyx_obj_4lxml_5etree__XSLTContext *__pyx_v_self) {
^
src/lxml/lxml.etree.c:141947:70: warning: incompatible pointer types passing 'struct __pyx_obj_4lxml_5etree__BaseContext *' to parameter of type 'struct __pyx_obj_4lxml_5etree__XSLTContext *' [-Wincompatible-pointer-types]
__pyx_t_1 = ((PyObject *)__pyx_f_4lxml_5etree_12_XSLTContext__copy(((struct __pyx_obj_4lxml_5etree__BaseContext *)__pyx_v_stylesheet->_context))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 680; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/lxml/lxml.etree.c:138476:138: note: passing argument to parameter '__pyx_v_self' here
static struct __pyx_obj_4lxml_5etree__BaseContext *__pyx_f_4lxml_5etree_12_XSLTContext__copy(struct __pyx_obj_4lxml_5etree__XSLTContext *__pyx_v_self) {
^
src/lxml/lxml.etree.c:160878:19: warning: expression result unused [-Wunused-value]
PyObject_INIT(o, t);
^
/Users/mmoncrief/anaconda/envs/py27/include/python2.7/objimpl.h:164:69: note: expanded from macro 'PyObject_INIT'
( Py_TYPE(op) = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
^
src/lxml/lxml.etree.c:162691:19: warning: expression result unused [-Wunused-value]
PyObject_INIT(o, t);
^
/Users/mmoncrief/anaconda/envs/py27/include/python2.7/objimpl.h:164:69: note: expanded from macro 'PyObject_INIT'
( Py_TYPE(op) = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
^
src/lxml/lxml.etree.c:168247:19: warning: expression result unused [-Wunused-value]
PyObject_INIT(o, t);
^
/Users/mmoncrief/anaconda/envs/py27/include/python2.7/objimpl.h:164:69: note: expanded from macro 'PyObject_INIT'
( Py_TYPE(op) = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
^
src/lxml/lxml.etree.c:170913:19: warning: expression result unused [-Wunused-value]
PyObject_INIT(o, t);
^
/Users/mmoncrief/anaconda/envs/py27/include/python2.7/objimpl.h:164:69: note: expanded from macro 'PyObject_INIT'
( Py_TYPE(op) = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
^
src/lxml/lxml.etree.c:12774:13: warning: function '__pyx_f_4lxml_5etree_displayNode' is not needed and will not be emitted [-Wunneeded-internal-declaration]
static void __pyx_f_4lxml_5etree_displayNode(xmlNode *__pyx_v_c_node, PyObject *__pyx_v_indent) {
^
8 warnings generated.
/usr/bin/clang -bundle -undefined dynamic_lookup -L/Users/mmoncrief/anaconda/envs/py27/lib -arch x86_64 -arch x86_64 build/temp.macosx-10.5-x86_64-2.7/src/lxml/lxml.etree.o -L/Users/mmoncrief/anaconda/envs/py27/lib -lxslt -lexslt -lxml2 -lz -lm -o build/lib.macosx-10.5-x86_64-2.7/lxml/etree.so
clang: error: unable to execute command: Segmentation fault: 11
clang: error: linker command failed due to signal (use -v to see invocation)
error: command '/usr/bin/clang' failed with exit status 254
----------------------------------------
Command /Users/mmoncrief/anaconda/envs/py27/bin/python -c "import setuptools;__file__='/private/var/folders/l2/zvz4t5rs7pb3n1bc4_bdz6n4cbzgwl/T/pip-build-mmoncrief/lxml/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/l2/zvz4t5rs7pb3n1bc4_bdz6n4cbzgwl/T/pip-vTKUXu-record/install-record.txt --single-version-externally-managed failed with error code 1 in /private/var/folders/l2/zvz4t5rs7pb3n1bc4_bdz6n4cbzgwl/T/pip-build-mmoncrief/lxml
Exception information:
Traceback (most recent call last):
File "/Users/mmoncrief/anaconda/envs/py27/lib/python2.7/site-packages/pip/basecommand.py", line 139, in main
status = self.run(options, args)
File "/Users/mmoncrief/anaconda/envs/py27/lib/python2.7/site-packages/pip/commands/install.py", line 271, in run
requirement_set.install(install_options, global_options, root=options.root_path)
File "/Users/mmoncrief/anaconda/envs/py27/lib/python2.7/site-packages/pip/req.py", line 1185, in install
requirement.install(install_options, global_options, *args, **kwargs)
File "/Users/mmoncrief/anaconda/envs/py27/lib/python2.7/site-packages/pip/req.py", line 592, in install
cwd=self.source_dir, filter_stdout=self._filter_install, show_stdout=False)
File "/Users/mmoncrief/anaconda/envs/py27/lib/python2.7/site-packages/pip/util.py", line 662, in call_subprocess
% (command_desc, proc.returncode, cwd))
InstallationError: Command /Users/mmoncrief/anaconda/envs/py27/bin/python -c "import setuptools;__file__='/private/var/folders/l2/zvz4t5rs7pb3n1bc4_bdz6n4cbzgwl/T/pip-build-mmoncrief/lxml/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/l2/zvz4t5rs7pb3n1bc4_bdz6n4cbzgwl/T/pip-vTKUXu-record/install-record.txt --single-version-externally-managed failed with error code 1 in /private/var/folders/l2/zvz4t5rs7pb3n1bc4_bdz6n4cbzgwl/T/pip-build-mmoncrief/lxml
任何帮助都非常感谢!
答案 0 :(得分:2)
请注意错误消息:
ImportError:似乎没有安装cssselect。看到 http://packages.python.org/cssselect/
cssselect
已被移动成为一个单独的包。尝试使用
pip install cssselect
答案 1 :(得分:0)
这对我有用,而不是通过以下方式安装lxml:
pip install lxml
尝试:
$CPATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/libxml2 pip install -U lxml
此处找到解决方案:http://unflyingobject.com/blog/stories/installing-lxml-on-109/