我对蟒蛇很陌生。我正试图从互联网上得到一个词的意思。独立的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似乎不一起使用。救命!
发布于 2014-09-30 09:04:49
我换成了urllib2和漂亮汤,这对pyxll很管用。下面是excel中数组函数的工作代码,它包含多个单词,并给出了两个含义。然而,CSS选择,我正在使用的是太限制,我还没有找到一个模式,我可以使用在网站上得到任何词的意义。
from pyxll import xl_func
import urllib2
from bs4 import BeautifulSoup
@xl_func("var[] x: var[]")
def dictionnaire(x):
height = len(x)
meanings = []
for i in range(height):
word = x[i][0]
row = []
url = "http://dictionnaire.reverso.net/francais-definition/"
url = url + word
page = urllib2.urlopen(url)
soup = BeautifulSoup(page)
results = soup.select("#ID0ENC")
row.append(results[0].get_text())
row.append(results[1].get_text())
meanings.append(row)
return meanings发布于 2014-09-09 16:19:30
你安装pywin32了吗?Python需要与Excel等Windows应用程序进行交互。
https://stackoverflow.com/questions/25748877
复制相似问题