Python requests not working on heroku as it worked on local host

Viewed 638

I have an api running on heroku. What my api does is fetches you results from amazon and puts it in the server for the respective request from flask. I am sorry because I have created that code for an organization in github and cannot reveal its code. But I can show some code to explain the problem. You can assume that all the variables are defined and no known problem or error in the code.

My flask code:
# flask_cors are enabled for all the requests.
from api.amazon import amazon_search

@app.route( '/amazon', method=["GET"] )
def amazonSearch():
    q = request.args["query"]
    result = amazon_search(q) # It returns a JSON
    return result

The api/amazon.py:

def amazon_search(keyword):
    link = f"https://www.amazon.com/s?k={keyword}&qid=1625886350&ref=sr_pg_2&page=1"
    headers = {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Accept-Language": "en-US,en;q=0.5",
        "Connection": "keep-alive",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; rv:89.0) Gecko/20100101 Firefox/89.0",
    }
    page = requests.get(link, headers=headers)
    soup = BeautifulSoup( page.content,"html.parser")
    # do some process and create a dict with variable `result`
    return result

I have already tried that, in my localhost and that works fine. But in Heroku, it does nor fetch any result. You can have a look at other working APIS:

  • IP
  • Random Password
  • Github Search You can see that all the above are Working, but the problem is with:
  • Amazon Search That shows no error, but also fetches nothing. All the APIs are running on same server, and even Github Search is done by requests and bs4, but Amazon Search is not working.
The fetch results from the apibu.herokuapp.com:
// url: https://apibu.herokuapp.com/amazon?query=Apple
{
  link: "/amazon?query=Apple&page=1",
  page: 1,
  query: "Apple",
  results: [ ],
  status: 200
}

But, when I run the same code on my local host, the result list is filled with 50 dicts. My Specifications:

Python: 3.9.6
Windows 10 32bit
Firefox (latest version)

All other specification are the latest one.

0 Answers
Related