获取随机词

问题描述 投票:1回答:2

我希望在当前的C#项目中使用随机选择的英语单词。我想到的一种方法是访问以下网站并检索随机生成的单词:

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

但是,我真的不知道该怎么做。到目前为止,我已经尝试了以下代码(无法正常工作):

string downloadedString;
WebClient client;

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

有人可以告诉我如何从给定的网站上获取一个或多个随机生成的单词。另外,如果有人知道一种更好的生成随机单词的方法,我也想听听。

c# php random webclient
2个回答
4
投票

使用相同的站点使用不同的URL,这将每次为您提供不同的51个名词集:

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

您可以使用此API,这是一个更好的主意。

http://randomword.setgetgo.com/

屏幕抓取是一个坏主意,因为屏幕可能会更改,从而破坏了解析实现。使用REST API更可靠,因为它不应该被更改。

您可以像这样使用上述API:

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

1
投票

由于该页面通过ajax加载了该单词,因此很难从那里抓住它。最好尝试使用该页面中的ajax调用的URL。

当前,API支持“类型”参数1(以逗号分隔的单词)或2(包括每个单词的定义)。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.