python - 执行错误 : The variable display is not defined. (-2753)

标签 python macos web-scraping beautifulsoup

我正在使用 beautiful soup 从网站中提取 2 组数据。但奇怪的是,我收到以下错误!

错误;

0:7: execution error: The variable display is not defined. (-2753)

代码:

import requests
import os
from bs4 import BeautifulSoup


word = []
meaning = []
r = requests.get("XYZ.com")
content = BeautifulSoup(r.content)
for words in content.findAll('span', attrs={'class':'qWord'}):
    word.append(unicode.join(u'\n',map(unicode,words.text)))

for word_meanings in content.findAll('span', attrs={'class':'qDef'}):
    meaning.append(unicode.join(u'\n',map(unicode,word_meanings.text)))

rest_command = 'display notification \"%s\" with title \"%s\"'.format(meaning[0],word[0])
os.system("osascript -e "+ rest_command)

该脚本基本上会获取多个单词及其含义,将它们存储在数组中,然后将它们显示为 OSX 通知。

OSX 通知引用:https://apple.stackexchange.com/questions/57412/how-can-i-trigger-a-notification-center-notification-from-an-applescript-or-shel

最佳答案

您需要在命令行中引用 AppleScript:

rest_command = """'display notification "{}" with title "{}"'""".format(meaning[0],word[0])
os.system("osascript -e "+ rest_command)

关于python - 执行错误 : The variable display is not defined. (-2753),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30102224/

相关文章:

python - 像素网格中的圆

python - SQLAlchemy IntegrityError 重复键违反唯一约束

c++ - 构造大尺寸的 vector<int>

swift - 如何从无限序列中获取 `Single<T>`?

python - 如何通过 Python 使用 Selenium 单击联赛列表中的每个项目时防止 StaleElementReferenceException

python - 如何在 Linux 中跟踪所有后代进程

python - 相互减去列表中的所有项目

macos - 没有任何布局约束的 View 可能会裁剪 - 使用 AutoresizingMask

python - 无法使用 scrapy 访问 xpath 属性

python - 使用 Python 的 BeautifulSoup 提取包含特定子字符串的 'a' 标签