我在Android应用中使用News API。
我正在尝试从服务器获取更多结果(新闻),但是它总是只返回20条结果,这些结果已被设置为文档here中提到的默认值。
这是我的代码:
class DownloadNews extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(String... args) {
String xml = "";
String urlParameters = "";
xml = Function.excuteGet("https://newsapi.org/v2/top-headlines?country=in&pageSize=100&apiKey=******************", urlParameters);
return xml;
}
@Override
protected void onPostExecute(final String xml) {
if (xml != null) {
if (xml.length() > 10) { // Just checking if not empty
Log.d("xml", xml);
} else {
}
} else {
}
}
}
我已将pageSize
自变量指定为100
,但仍然得到20
totalResults
:
09-29 22:29:22.241 10275-10275/com.abc.xyz D/xml: {"status":"ok","totalResults":20,"articles":[]}
这是怎么了?
答案 0 :(得分:2)
/ top-headlines端点最多只能返回20条文章。当不再将某文章视为“头条新闻”时,该文章就会落在该终点之外。如果您需要检索20篇以上的文章,则可以使用/everything endpoint,其中包括所有头条新闻以及任何其他较小的文章。
答案 1 :(得分:1)
这是API出现问题。似乎只有 20 个新闻条目可用于“ 印度”(如果“ in ”代表该新闻),则 20 似乎是经过硬编码的,或者返回的项目总数与 pageSize 参数无关。
在Postman中,我像您一样尝试了pageSize=100
,但仍然得到了"totalResults": 20
。我再次尝试了pageSize=10
,有趣的是,totalResults
仍然是20,但是却又按规定返回了10篇文章。
答案 2 :(得分:1)
API的工作正常,我刚刚实现了它,仅将页面视为1,2 ...等等,即当您获得前20条记录(或按页面大小)时,将页面设置为1,然后递增页面第二个请求。
我很快在下面实现了它
{{1}}
希望对其他人有帮助!!!!
答案 3 :(得分:0)
答案 4 :(得分:0)
这是NewAPI的限制。设置
后,我得到了这个pageSize=100
&page=100
这是结果。
{'status': 'error', 'code': 'maximumResultsReached', 'message': 'You have requested too many results. Developer accounts are limited to a max of 100 results deep. You are trying to request results 9900 to 10000. Please upgrade to a paid plan if you need more results.'}
Error reading
这不是编程问题。
答案 5 :(得分:0)
您最好使用ContextualWeb NewsAPI。结果更好,更受欢迎。同样,您不限于20个结果。他们的NewsAPI是RapidAPI市场上最受欢迎的API。
代码可在此处找到:https://github.com/roikra/newsapi
APIKey可以在这里生成:https://rapidapi.com/contextualwebsearch/api/web-search
代码如下:
public static void Test()
{
//Required Package: Unirest-API (https://www.nuget.org/packages/Unirest-API/)
//Replace the following string value with your valid X-RapidAPI-Key.
string Your_X_RapidAPI_Key = "xxxxxxxxxxxxxxxxxxxxx";
//The query parameters: (update according to your search query)
string q = "Taylor%20Swift"; //the search query
int pageNumber = 1; //the number of requested page
int pageSize = 10; //the size of a page
bool autoCorrect = true; //autoCorrectspelling
bool safeSearch = false; //filter results for adult content
//Perform the web request and get the response
var response = Unirest.get(string.Format("https://contextualwebsearch-websearch-v1.p.rapidapi.com/api/Search/NewsSearchAPI?q={0}&pageNumber={1}&pageSize={2}&autoCorrect={3}&safeSearch={4}", q, pageNumber, pageSize, autoCorrect, safeSearch))
.header("X-RapidAPI-Key", Your_X_RapidAPI_Key)
.asJson<string>();
//Get the ResponseBody as a JSON
dynamic jsonBody = JsonConvert.DeserializeObject(response.Body);
//Parse the results
//Get the numer of items returned
int totalCount = (int)jsonBody["totalCount"];
//Get the list of most frequent searches related to the input search query
List<string> relatedSearch = JsonConvert.DeserializeObject<List<string>>(jsonBody["relatedSearch"].ToString());
//Go over each resulting item
foreach (var webPage in jsonBody["value"])
{
//Get the web page metadata
string url = webPage["url"].ToString();
string title = webPage["title"].ToString();
string description = webPage["description"].ToString();
string keywords = webPage["keywords"].ToString();
string provider = webPage["provider"]["name"].ToString();
DateTime datePublished = DateTime.Parse(webPage["datePublished"].ToString());
//Get the web page image (if exists)
string imageUrl = webPage["image"]["url"].ToString();
int imageHeight = (int)webPage["image"]["height"];
int imageWidth = (int)webPage["image"]["width"];
//Get the web page image thumbail (if exists)
string thumbnail = webPage["image"]["thumbnail"].ToString();
int thumbnailHeight = (int)webPage["image"]["thumbnailHeight"];
int thumbnailidth = (int)webPage["image"]["thumbnailWidth"];
//An example: Output the webpage url, title and published date:
Console.WriteLine(string.Format("Url: {0}. Title: {1}. Published Date:{2}.",
url,
title,
datePublished));
}
}
答案 6 :(得分:0)
我最近完成了python news api project的网络抓取部分的开发,这将使您可以不受限制地直接从新闻网站提取数据。
要安装:
pip -q install git+https://github.com/mansaluke/newsai.git
这是一个示例脚本: https://github.com/mansaluke/newsai/blob/master/examples/get_news_data.py
让我知道您的想法并喜欢?!