Harrison Cox
Harrison Cox

Reputation: 67

UnicodeEncodeError: 'ascii' codec can't encode character '\u2013' in position 13: ordinal not in range(128)

I have used the SerpApi to web scrape from google product search. The API key has been taken out for obvious reasons. Every time I run the program, this error keeps coming up. How would I stop this from happening?

import json
from serpapi import GoogleSearch

params = {
  "api_key": "api_key",
  "engine": "google",
  "q": "marvel",
  "gl": "uk",
  "hl": "en",
  "tbm": "shop"
}

search = GoogleSearch(params)
results = search.get_dict()

for i in range(3):
  for shopping_result in results['shopping_results']:
        try:
              title = shopping_result['title']  #Finds relating title
        except:
              title = "None"
        try:
              source = shopping_result['source']  #Finds relating title
        except:
              source = "None"
        try:
              price = shopping_result['price']  #Finds relating title
        except:
              price = "None"

        print(title)
        print(source)
        print(price)

This is the error that I recieve

Upvotes: 1

Views: 728

Answers (1)

Adon Bilivit
Adon Bilivit

Reputation: 27211

I have simplified the code and removed the pointless range iteration. I have created a REPL on replit.com and this executes without error:

from serpapi import GoogleSearch

params = {
    "api_key": "<my api key>",
    "engine": "google",
    "q": "marvel",
    "gl": "uk",
    "hl": "en",
    "tbm": "shop"
}

results = GoogleSearch(params).get_dict()


for shopping_result in results.get('shopping_results', []):
    print(shopping_result.get('title', 'None'))
    print(shopping_result.get('source', 'None'))
    print(shopping_result.get('price', 'None'))

Upvotes: 1

Related Questions