Search code examples
pythonpython-3.xrequestautosuggest

Python: use request to get the result of auto suggestion from a webpage


I am looking at this web page. In that page, there is a small box that says GET QUOTE.

If I for example type AMD the auto-suggestion opens up and shows a list like this: enter image description here

My question is how to use request in Python3 to get this list, meaning to get:

AMD      Advanced Micro Devices
AMDA     Amedia Corp

Thanks for the help.


Solution

  • You can use browser's debugging facility to see what's going on when you request, and what you've got. For example, in Chrome, you can use Network tab of Developer Tools to see what request/response are made.

    Chromium - Developer Tools - Network


    Use json parameter to send application/json request, and use Response.json() to decode the json response text:

    >>> import requests
    >>> url = 'http://research.investors.com/services/AutoSuggest.asmx/GetQuoteResults'
    >>> response = requests.post(url, json={'q':'AMD','limit':10})
    >>> data = response.json()
    >>> [row['Symbol'] for row in data['d']]
    ['AMD', 'AMDA', 'DOX']