Google suggest query using curl

核能气质少年 提交于 2019-12-14 02:34:26

问题


When I am doing request using different browser it is giving me proper xml response.

http://suggestqueries.google.com/complete/search?output=toolbar&hl=en&q=test&gl=US

Now I am request above url using curl command but it is giving me 400 error:

curl http://suggestqueries.google.com/complete/search?output=toolbar&hl=en&q=test&gl=US

Also I have passed user-agent but it is also giving me same error.

curl -A "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:27.0) Gecko/20100101 Firefox/27.0"  http://suggestqueries.google.com/complete/search?output=toolbar&hl=en&q=test&gl=US

Please guide If I missing somethings.

ERROR

400. That’s an error.

Your client has issued a malformed or illegal request. That’s all we know.

When I am trying below command:

curl -v -A "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:27.0) Gecko/20100101 Firefox/27.0" -H "Accept: application/xml" -H "Content-Type: application/xml" https://suggestqueries.google.com/complete/search?hl=en&q=test&gl=US&format=rich

Then getting response:

  • About to connect() to suggestqueries.google.com port 443 (#0)
  • Trying 74.125.200.138... connected
  • successfully set certificate verify locations:
  • CAfile: none CApath: /etc/ssl/certs
  • SSLv3, TLS handshake, Client hello (1):
  • SSLv3, TLS handshake, Server hello (2):
  • SSLv3, TLS handshake, CERT (11):
  • SSLv3, TLS handshake, Server key exchange (12):
  • SSLv3, TLS handshake, Server finished (14):
  • SSLv3, TLS handshake, Client key exchange (16):
  • SSLv3, TLS change cipher, Client hello (1):
  • SSLv3, TLS handshake, Finished (20):
  • SSLv3, TLS change cipher, Client hello (1):
  • SSLv3, TLS handshake, Finished (20):
  • SSL connection using ECDHE-ECDSA-AES128-SHA
  • Server certificate:
  • subject: C=US; ST=California; L=Mountain View; O=Google Inc; CN=*.google.com
  • start date: 2014-08-27 11:47:20 GMT
  • expire date: 2014-11-25 00:00:00 GMT
  • subjectAltName: suggestqueries.google.com matched
  • issuer: C=US; O=Google Inc; CN=Google Internet Authority G2
  • SSL certificate verify ok.

  • GET /complete/search?hl=en HTTP/1.1

  • User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:27.0) Gecko/20100101 Firefox/27.0
  • Host: suggestqueries.google.com
  • Accept: application/xml
  • Content-Type: application/xml

  • HTTP/1.1 200 OK

  • Content-Type: text/html; charset=UTF-8
  • Set-Cookie: PREF=ID=221163cf51f1fd63:FF=0:TM=1410889133:LM=1410889133:S=8a7TwxXZ4a-1bM3l; expires=Thu, 15-Sep-2016 17:38:53 GMT; path=/; domain=.google.com
  • Set-Cookie: NID=67=UiY78HgmkOLxvy0S4_EfmD7vRX0gInDI6FhWWuj2cP9w25NfV6DdzVq7XooHlo5QNH1NteyjxWqyDVm0Dp9OD8Y7ABx-GEOy-wYEHDEciDmIxFhCFxixOXO1RJIn5UL_; expires=Wed, 18-Mar-2015 17:38:53 GMT; path=/; domain=.google.com; HttpOnly
  • P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
  • Date: Tue, 16 Sep 2014 17:38:53 GMT
  • Server: gws
  • Content-Length: 0
  • X-XSS-Protection: 1; mode=block
  • X-Frame-Options: SAMEORIGIN
  • Expires: Tue, 16 Sep 2014 17:38:53 GMT
  • Cache-Control: private
  • Alternate-Protocol: 443:quic,p=0.002

  • Connection #0 to host suggestqueries.google.com left intact

  • Closing connection #0
  • SSLv3, TLS alert, Client hello (1):

  • Done curl -v -A "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:27.0) Gecko/20100101 Firefox/27.0" -H "Accept: application/xml" -H "Content-Type: application/xml" https://suggestqueries.google.com/complete/search?hl=en

  • Done q=test
  • Done gl=US

回答1:


Get wireshark and check the packages. It could be useless because I can see that curl use SSL. However you can check if your browser use SSL too. If not you get the full package that you can compare to curl output and find out which field cause error(check responses from google too, they may vary not only by 400 response).




回答2:


You have to put the url in quotes, try:

curl 'http://suggestqueries.google.com/complete/search?output=toolbar&hl=en&q=test&gl=US'

the & is treated specially in most shells.

As you can see in the error you posted, the url is cut before the first &:

Done curl -v -A "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:27.0) 
Gecko/20100101 Firefox/27.0" -H "Accept: application/xml" -H "Content-Type: 
application/xml" https://suggestqueries.google.com/complete/search?hl=en
                                                                       ^
                                                                       |


来源:https://stackoverflow.com/questions/25874492/google-suggest-query-using-curl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!