Reputation: 67
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)
Upvotes: 1
Views: 728
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