2024-05-15 22:56:27 发布
网友
我试图追踪不同单词进入英语词典的日期(牛津、韦氏等)
我最理想的情况是找到一个API,这样如果我发送了“selfie”,我会返回“2013”作为回复的一部分,但我认为这些数据并不常见,甚至不容易访问。在
我看过:
但这两个网站似乎都没有提供访问这些数据的途径。在
我正在使用python,并且已经查看了pypi.python.org,但是还没有找到任何可以解决这个问题的模块。在
python
看起来我可能只需要手工收集我正在寻找的数据,但在此之前,我想看看是否有人知道更好的方法来处理这件事。在
这是检索单词的基本脚本。如果一个词有多个定义,它只选择一个。在
import re import urllib2 # Import Custom libraries from BeautifulSoup import BeautifulSoup def render_oxford_uri(term): ''' Render the appropriate Oxford request uri ''' base_url = "http://www.oxforddictionaries.com/definition/english/" url = base_url + ("%s?q=%s" % (re.sub("\s+", "-", term), re.sub("\s+", "+", term))) return url def get_words(*args): ''' Oxford dictionary word scraper ''' ret_list = [] for term in args: request_uri = render_oxford_uri(term) request = urllib2.Request(request_uri, None, {}) try: response = urllib2.urlopen(request) the_page = response.read() except Exception: the_page = "" if the_page: pool = BeautifulSoup(the_page) result = pool.find("div", attrs={"class" : "entryPageContent"}) if result: term = result.find("h2" , attrs={"class" : "pageTitle"}) speech_part = result.find("span", attrs={"class" : "partOfSpeech"}) definition = result.find("span", attrs={"class" : "definition"}) date = result.find("span", attrs={"class" : "date"}) cur_dict = \ { "Term" : term.text, "Part of Speech" : speech_part.text, "Definition" : definition.text, "Date" : date.text, } ret_list += [cur_dict] return ret_list if __name__ == "__main__": print get_words("selfie", "vapid")
这是检索单词的基本脚本。如果一个词有多个定义,它只选择一个。在
相关问题 更多 >
编程相关推荐