Pergunta

I am looking to use randomly selected English words in my current C# project. One way I have thought of doing this is to access the following website and retrieve a randomly generated word:

http://www.wordgenerator.net/random-word-generator.php

However, I don't really know how to do this. So far I have tried the following code (which doesn't work as intended):

string downloadedString;
WebClient client;

client = new WebClient();
downloadedString = client.DownloadString("http://www.wordgenerator.net/random-word-generator.php#rname");

Could someone please show me how to get a randomly generated word/words from the website given. Also, if someone knows a better method for generating random words I would like to hear that too.

Foi útil?

Solução

Using the same site with a different URL, this will give you a different set of 51 nouns each time:

client = new WebClient();
downloadedString = client.DownloadString("http://www.wordgenerator.net/application/p.php?id=nouns&type=50&spaceflag=false");            
string[] randomWords = downloadedString.Split(',');

You can consume this API which will be a better idea.

http://randomword.setgetgo.com/

Screen scraping is a bad idea as screens can change breaking your parsing implementation. Consuming a REST API is more reliable as it should be less prone to change.

You can consume the above API like this:

client = new WebClient();
downloadedString = client.DownloadString("http://randomword.setgetgo.com/get.php");            
string randomWord = downloadedString;

Outras dicas

Since that page load that word via ajax, it will be hard to catch it from there. Best try out with the url called by the ajax in that page.

Currently the API supports "type" parameter 1 (words comma separated) or 2 (including the definition for each word).

http://www.wordgenerator.net/application/p.php?id=dictionary_words&type=1&spaceflag=false

or

http://www.wordgenerator.net/application/p.php?id=dictionary_words&type=2&spaceflag=false

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top