C++到底怎么输出"啊


  • ACG

    Google Custom Search API貌似没有C/C++版本,只有Java(好像还有Python),于是我想借助Powershell打开URL
    但是Google Advanced Search中参数是用&连接的,而Powershell中不支持&,所以URL必须用"包括起来
    e.g.

    $ start "https://www.google.com/search?&as_q=c++&as_sitesearch=umr.fun"
    

    目前我还没加"的代码版本是

    //...
    	string cmd = "start https://www.google.com/search?";	//TO DO: add " before and behind URL
    //...
    	cmd += cfg;
    	char * p_cmd = (char *)cmd.data();
    	system(p_cmd);	//TO DO: open cli-web-browser instead
    //...
    

    现在的问题是,按照网上的说法,用"输出"的话,第一句应该写成

    	string cmd = "start \"https://www.google.com/search?"
    

    但是这样VS会把start \赋值给cmd,忽略后面的https...,\并没有起到转义的作用,效果是打开文件所在目录
    所以,到底怎么把这对双引号加上啊...

    另外,我打算借助Google相关API抓取搜索结果的title和URL,直接在命令行显示,不过目前好像没精力做,预期的样子:

    ╔=======================╗
    ‖Google Advanced Search ‖
    ╚=======================╝
    C++到底怎么输出"啊 site:umr.fun
    
    0.exit
    1.aflkjklj
    2.hakjlgh
    3.zkjhfia
    4.nzkljla
    5.nfdgief
    6.zcvhui
    7.fnjhsl
    8.jlhafd
    9.nvoahl
    10.dafw4n
    
    |<-	<-	[page]	      ->     ->|
    Home	PageUp		PageDown     End
    //输入结果编号打开指定URL,利用home,pageup,pagedown,end翻页
    

    目前的源码(因为这个问题没解决,还未上传GitHub):

    //main.cpp
    
    #include <iostream>
    #include <Windows.h>
    #include <string>
    
    using namespace std;
    
    void all_these_words();
    void this_exact_word_or_phrase();
    void this_exact_word_or_phrase();
    void any_of_these_words();
    void none_of_these_words();
    void numbers_ranging_from();
    void language();
    void region();
    void last_update();
    void site_or_domin();
    void terms_appearing();
    void SafeSearch();
    void file_type();
    void usage_rights();
    
    string cmd = "start https://www.google.com/search?";	//TO DO:add " before and behind URL
    
    string cfg = "";
    
    int main()
    {
    	cout	<< "00:exit"									<< endl
    			<< "01.all these words"					<< endl
    			<< "02.this exact word or phrase"	<< endl
    			<< "03.any of these words"			<< endl
    			<< "04.none of these words"			<< endl
    			<< "05.numbers ranging from"		<< endl
    			<< "06.language"							<< endl
    			<< "07.region"								<< endl
    			<< "08.last update"						<< endl
    			<< "09.site or domin"						<< endl
    			<< "10.terms appearing"				<< endl
    			<< "11.SafeSearch"							<< endl
    			<< "12.file type"								<< endl
    			<< "13.usage rights"						<< endl;
    	
    	int i = 0;
    	cin >> i;
    	while(i!=0)
    	{
    		switch (i)
    		{
    		case 1:	all_these_words(); break;
    		case 2:	this_exact_word_or_phrase(); break;
    		case 3:	any_of_these_words(); break;
    		case 4:	none_of_these_words(); break;
    		case 5:	numbers_ranging_from(); break;
    		case 6:	language(); break;
    		case 7:	region(); break;
    		case 8:	last_update(); break;
    		case 9:	site_or_domin(); break;
    		case 10:	terms_appearing(); break;
    		case 11:	SafeSearch(); break;
    		case 12:	file_type(); break;
    		case 13:	usage_rights(); break;
    		default:	
    			cout << "error!" << endl << endl;
    			main();
    		}
    
    		cout << "Configuration:" << cfg << endl;
    		//cout << "command:" << cmd + cfg << endl;
    
    		cin >> i;
    	} 
    
    	cmd += cfg;
    	char * p_cmd = (char *)cmd.data();
    	system(p_cmd);	//TO DO:open cli-web-browser instead
    
    	//getchar();
    	return 0;
    }
    
    void all_these_words()
    {
    	string as_q = "";
    	cout << "Type the important words:";
    	cin >> as_q;
    	cfg += "&as_q=" + as_q;
    }
    
    void this_exact_word_or_phrase()
    {
    	string as_epq = "";
    	cout << "Put exact words in quotes:";
    	cin >> as_epq;
    	cfg += "&as_epq=" + as_epq;
    }
    
    void any_of_these_words()
    {
    	string as_oq = "";
    	cout << "Type OR between all the words you want:";
    	cin >> as_oq;
    	cfg += "&as_oq=" + as_oq;
    }
    
    void none_of_these_words()
    {
    	string as_eq = "";
    	cout << "Put a minus sign just before words you don't want:";
    	cin >> as_eq;
    	cfg += "&as_eq=" + as_eq;
    }
    
    void numbers_ranging_from()
    {
    	string as_nlo = "", as_nhi = "";
    	cout << "Put 2 periods between the numbers and add a unit of measure:";
    	cin >> as_nlo >> as_nhi;
    	cfg += "&as_nlo=" + as_nlo + "&as_nhi=" + as_nhi;
    }
    
    void language()
    {
    	string lr = "";
    	cout << "Find pages in the language you select:";
    	cin >> lr;
    	cfg += "&lr=" + lr;
    }
    
    void region()
    {
    	string cr = "";
    	cout << "Find pages published in a particular region:";
    	cin >> cr;
    	cfg += "&cr=" + cr;
    }
    
    void last_update()
    {
    	string as_qdr = "all";
    	cout << "Find pages updated within the time you specify:";
    	cin >> as_qdr;
    	cfg += "&as_qdr=" + as_qdr;
    }
    
    void site_or_domin()
    {
    	string as_sitesearch = "";
    	cout << "Search one site or limit your results to a domain:";
    	cin >> as_sitesearch;
    	cfg += "&as_sitesearch=" + as_sitesearch;
    }
    
    void terms_appearing()
    {
    	string as_occt = "any";
    	cout << "Search for terms in the whole page, page title, or web address, or links to the page you're looking for:";
    	cin >> as_occt;
    	cfg += "&as_occt=" + as_occt;
    }
    
    void SafeSearch()
    {
    	string safe = "images";
    	cout << "Tell SafeSearch whether to filter sexually explicit content:";
    	cin >> safe;
    	cfg += "&safe=" + safe;
    }
    
    void file_type()
    {
    	string as_filetype = "";
    	cout << "Find pages in the format you prefer:";
    	cin >> as_filetype;
    	cfg += "&as_filetype=" + as_filetype;
    }
    
    void usage_rights()
    {
    	string as_rights = "";
    	cout << "Find pages you are free to use yourself:";
    	cin >> as_rights;
    	cfg += "&as_rights=" + as_rights;
    }
    

Log in to reply