Web Scraping Help

For my game I would like to retrieve a random word along with a definition from a website/API.
The website I would like to get the random word and definition from is ‘https://randomword.com’.

I cant seem to find an API linked to this website so I can make my own requests. I can seem to scrape the data by doing response = HTTPService:GetAsync(url) and printing it raw.
The image shows the response I get.
image

I know in other languages such as python you can access the classes and id’s to get the data you want but I don’t know how I would go about this in Lua.

I have checked other forums post and the closest solution I could find was someone referencing another API ‘https://random-word-api.herokuapp.com/home’. This solution worked until the website eventually crashed.
My game is based around random words so whenever I make multiple requests to this website I get error codes (error 503 - server unable to handle request).

Any help is much appreciated, thanks.

local HttpService = game:GetService("HttpService")
local response = HttpService:GetAsync("http://randomword.com")

--Remove the first part of the html file
local charAmount1 = string.find(response, "<div id=\"random_word\">")
local randomWord = string.sub(response, charAmount1, string.len(response))

--Remove the part after the random word
local charAmount2 = string.find(randomWord, "\n") --Finds where the next line begins
randomWord = string.sub(randomWord, 1, charAmount2)

--Remove <div id...>
randomWord = string.gsub(randomWord, "<div id=\"random_word\"", "")
randomWord = string.gsub(randomWord, "</div>\n", "")

print(randomWord)
2 Likes

This seems to be the best way to go about it and it works. Thanks.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.