用Python抓取urbandictionary

2024-10-01 15:33:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在正在研究一个arcbot,我正在尝试发出一个命令“!“urbandictionary”,它应该获取一个术语的含义,第一个是urbandictionary提供的,如果有其他解决方案,例如,另一个字典站点有一个更好的api,这也很好。我的代码是:

if Command.lower() == '!urban': 
    dictionary = Argument[1] #this is the term which the user provides, e.g. "scrape"
    dictionaryscrape = urllib2.urlopen('http://www.urbandictionary.com/define.php?term='+dictionary).read() #plain html of the site
    scraped = getBetweenHTML(dictionaryscrape, '<div class="meaning">','</div>') #Here's my problem, i'm not sure if it scrapes the first meaning or not..
    messages.main(scraped,xSock,BotID) #Sends the meaning of the provided word (Argument[0])

如何在urbandictionary中正确地提取单词的意思?在


Tags: ofthe命令divdictionaryifnotargument
2条回答

只需从meaning类中获取text

import  requests

from bs4 import BeautifulSoup
word = "scrape"
r = requests.get("http://www.urbandictionary.com/define.php?term={}".format(word))

soup = BeautifulSoup(r.content)

print(soup.find("div",attrs={"class":"meaning"}).text)

Gassing and breaking your car repeatedly really fast so that the front and rear bumpers "scrape" the pavement; while going hyphy 

这里显然有一个非官方的api

`http://api.urbandictionary.com/v0/define?term={word}`

来自https://github.com/zdict/zdict/wiki/Urban-dictionary-API-documentation

相关问题 更多 >

    热门问题