gpt4 book ai didi

python - 用 Python 抓取城市词典

转载 作者:太空宇宙 更新时间:2023-11-04 10:26:45 25 4
gpt4 key购买 nike

我目前正在研究一个 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 类中获取 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

关于python - 用 Python 抓取城市词典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28941413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com