如何获得网站上出现的词典的跨度? [英] How to get the span of a dictionary as it appears on the site?
问题描述
我正在尝试获取用户输入的单词的名词"标题中的所有含义. 这是我现在的代码:
I am trying to get all the meanings in the "noun" heading of the word the user enters. This is my code for now:
import requests
from bs4 import BeautifulSoup
word=raw_input("Enter word: ").lower()
url=('http://www.dictionary.com/browse/'+word)
r=requests.get(url)
soup=BeautifulSoup(r.content,"html.parser")
try:
meaning=soup.find("div",attrs={"class":"def-content"}).get_text()
print "Meaning of",word,"is: "
print meaning
except AttributeError:
print "Sorry, we were not able to find the word."
pass
finally:
print "Thank you for using our dictionary."
现在假设用户输入单词"today
",我的输出将是:
Now suppose the user enters the word "today
" and my output will be:
this present day: Today is beautiful.
我不明白为什么它会留出这么多空间,为什么零件不会
I dont understand why does it leave so many spaces and why doesnt the part
今天很美好"
"Today is beautiful"
下来.
无论如何,当您在网站上查询该单词时,您会发现有2种含义,但我的程序只显示一种.
我希望输出为:
come down.
Anyway when you look up that word on this site, you can see there are 2 meanings yet my program only shows one.
I want the output to be:
1.this present day:
Today is beautiful.
2.
this present time or age:
the world of today.
谁能向我解释我怎么了?我该如何解决?
我不知道怎么了,所以请不要以为我尝试.
Can anyone explain me whats wrong and how can i fix it?
I have no idea what's wrong so please dont think I dint try.
推荐答案
使用上述代码,您将获得第一个名词的含义. 我已经重写了代码,如下所示:
You are getting the first noun meaning using the above code. I have rewritten the code, it is as below:
from bs4 import BeautifulSoup
import requests
word = raw_input("Enter word: ").lower()
url = ('http://www.dictionary.com/browse/' + word)
r = requests.get(url)
bsObj = BeautifulSoup(r.content, "lxml")
nouns = bsObj.find("section", {"class": "def-pbk ce-spot"})
data = nouns.findAll('div', {'class': 'def-content'})
count = 1
for item in data:
temp = ' '.join(item.get_text().strip().split())
print str(count) + '. ' + temp
count += 1
说明:
是的.假设网站首先显示名词含义,那么我正在检索包含完整名词数据的第一部分.然后,我在数据变量内的该部分下找到所有含义,并在循环中对其进行迭代,并获取数据中存在的每种含义的文本.然后,要删除所有多余的空格,我将读取的文本拆分并用一个空格将其连接起来,并在开头添加一个数字
Yes. Assuming the website shows noun meaning first, I am retrieving the first section which contains complete noun data. Then I am finding all the meanings under that section inside data variable and iterating it in a loop and fetching the text of each meaning present in the data. Then to remove all the extra spaces I am splitting the fetched text and the joining it with a single space along with the addition of a number at the beginning
这篇关于如何获得网站上出现的词典的跨度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!