首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在尝试wikipedia查询时CURLPP 400坏请求

在尝试wikipedia查询时CURLPP 400坏请求
EN

Stack Overflow用户
提问于 2015-09-11 11:19:18
回答 1查看 355关注 0票数 1

我希望使用以下URL提取维基百科页面的摘要:

代码语言:javascript
复制
https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=Stack%20Overflow

我还利用CURLPP图书馆来实现这一目标:

代码语言:javascript
复制
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
#include <curlpp/Exception.hpp>
#include <sstream>
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
    string query = "Aldous Huxley";
    curlpp::Cleanup myCleanup;
    std::ostringstream os;
    string url = "https://en.wikipedia.org/w/api.php?format=xml&action=query&prop=extracts&exintro=&explaintext=&titles=";
    os << curlpp::options::Url(std::string( url + query));
    
    string asAskedInQuestion = os.str();
    cout << asAskedInQuestion << endl;
   return 0;
}

输出

每次尝试查询Aldous Huxley时,我都会得到以下错误:

代码语言:javascript
复制
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.9.4</center>
</body>
</html>

任何其他查询都正常工作。我为什么要犯这个错误?例如,当我尝试无政府主义时,我得到以下输出:

代码语言:javascript
复制
<api batchcomplete="">
<query>
<pages>
<page _idx="12" pageid="12" ns="0" title="Anarchism">
<extract xml:space="preserve">
Anarchism is a political philosophy that advocates stateless societies often defined as self-governed voluntary institutions, but that several authors have defined as more specific institutions based on non-hierarchical free associations. Anarchism holds the state to be undesirable, unnecessary, or harmful. While anti-statism is central, anarchism entails opposing authority or hierarchical organisation in the conduct of human relations, including, but not limited to, the state system. As an anti-dogmatic philosophy, anarchism draws on many currents of thought and strategy. Anarchism does not offer a fixed body of doctrine from a single particular world view, instead fluxing and flowing as a philosophy. There are many types and traditions of anarchism, not all of which are mutually exclusive. Anarchist schools of thought can differ fundamentally, supporting anything from extreme individualism to complete collectivism. Strains of anarchism have often been divided into the categories of social and individualist anarchism or similar dual classifications. Anarchism is usually considered a radical left-wing ideology, and much of anarchist economics and anarchist legal philosophy reflect anti-authoritarian interpretations of communism, collectivism, syndicalism, mutualism, or participatory economics.
</extract>
</page>
</pages>
</query>
</api>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-11 11:34:46

您需要对查询字符串进行URL编码。使用,例如。

代码语言:javascript
复制
string query = "Aldous%20Huxley";
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32522207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档