I'm working on a script that will scrape any answers to my questions using DuckDuckGo, I tried doing this using DuckDuckGo's API. and it kind of works as well but the result gives a ton of info.? Is there any method to limit it's sentences? Like to 3 sentences or 4 senteces: this is my script till now:
word = input("Enter the Word: ")
query = f"what is {word}?"
r = requests.get("https://api.duckduckgo.com",
params = {
"q": query,
"format": "json"
})
data = r.json()
print(data["Abstract"])
For example, if you want to limit RelatedTopics count, you can use something like this:
def limit_count_topicks(n):
for i in range(len(data['RelatedTopics'])):
if i == n:
break
print(data['RelatedTopics'][i])
limit_count_topicks(2)
Output only 2 related topicks:
{'FirstURL': 'https://duckduckgo.com/Happiness', 'Icon': {'Height': '', 'URL': '/i/a62c4a70.png', 'Width': ''}, 'Result': '<a href="https://duckduckgo.com/Happiness">Happiness</a>The term happiness is used in the context of mental or emotional states, including positive or...', 'Text': 'Happiness The term happiness is used in the context of mental or emotional states, including positive or...'}
{'FirstURL': 'https://duckduckgo.com/Happy!_(TV_series)', 'Icon': {'Height': '', 'URL': '', 'Width': ''}, 'Result': '<a href="https://duckduckgo.com/Happy!_(TV_series)">Happy! (TV series)</a>Happy! is an American live-action/adult animated black comedy/action-drama television series...', 'Text': 'Happy! (TV series) Happy! is an American live-action/adult animated black comedy/action-drama television series...'}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.