用 Python 抓取 urbandictionary
Scraping urbandictionary with 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 中正确抓取单词的含义?
只需从 meaning
class 中获取 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
我目前正在开发一个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 中正确抓取单词的含义?
只需从 meaning
class 中获取 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