Can I tell pytrends to skip a search term if there's no data for that term?

Viewed 136

Having an issue with the following script:

import pandas as pd
from pytrends.request import TrendReq
pytrend = TrendReq(hl='en-AU', tz=360)
keywords = pd.read_csv("keywordlist.txt")
pytrend.build_payload(
     kw_list=keywords,
     cat=0,
     geo='AU',
     gprop='')
data = pytrend.interest_over_time()
data.to_csv('KeywordList.csv', encoding='utf_8_sig')

If Google Trends doesn't have any data for a particular search query, the script throws an error. Works fine if there is data, naturally.

The issue I'm having is that when utilising a list ("keywordlist.txt"), the script is producing an empty .csv, even though I know for a fact that some of the search terms do have Google Trends data.

Is there a change I could make so that if there is no data for a particular search term, that row is simply skipped and it moves onto the next?

The ideal outcome would be a .csv file that only features the search terms that have data (and their associated interest metrics).

0 Answers
Related