我是 python 新手。我正在尝试从互联网上获取单词的含义。独立的 python 代码工作得很好。
from lxml import html
import requests
url = "http://dictionnaire.reverso.net/francais-definition/"
word = raw_input("please enter the word you want to translate ")
url = url + word
page = requests.get(url)
tree= html.fromstring(page.text)
translation = tree.xpath('//*[@id="ID0EYB"]/text()')
print translation
请注意,我使用的 xpath 仅用于测试目的。与“manger”、“gonfler”等简单的词配合使用效果很好。我正在尝试的下一步是使用 pyxll addin for excel 在 excel 中为同一任务创建一个函数。
from pyxll import xl_func
from lxml import html
import requests
@xl_func("string x: string")
def traduction(x):
url = "http://dictionnaire.reverso.net/francais-definition/"
url = url + x
page = requests.get(url)
tree= html.fromstring(page.text)
translation = tree.xpath('//*[@id="ID0EYB"]/text()')
return translation
在此之后,当我启动 excel 时,出现错误。在pyxll的日志文件中,错误描述如下:
2014-09-09 17:02:41,845 - ERROR : Error importing 'worksheetfuncs': DLL load failed: Le module spécifié est introuvable.
2014-09-09 17:02:41,845 - ERROR : Traceback (most recent call last):
2014-09-09 17:02:41,845 - ERROR : File "pyxll", line 791, in _open
2014-09-09 17:02:41,845 - ERROR : File "\pyxll\examples\worksheetfuncs.py", line 317, in <module>
2014-09-09 17:02:41,845 - ERROR : from lxml import html
2014-09-09 17:02:41,846 - ERROR : File "C:\Python27\lib\site-packages\lxml\html\__init__.py", line 42, in <module>
2014-09-09 17:02:41,846 - ERROR : from lxml import etree
2014-09-09 17:02:41,846 - ERROR : ImportError: DLL load failed: Le module spécifié est introuvable.
2014-09-09 17:02:41,888 - WARNING : pydevd failed to import - eclipse debugging won't work
2014-09-09 17:02:41,888 - WARNING : Check the eclipse path in \pyxll\examples\tools\eclipse_debug.pyc
2014-09-09 17:02:41,890 - INFO : callbacks.license_notifier: This copy of PyXLL is for evaluation or non-commercial use only
我已经使用带有 API 的翻译网站来做类似的事情,而且效果很好。对我来说真正的问题是我使用 lxml 的解析,似乎 lxml 和 pyxll 不能一起使用。请帮忙!!!