I want to download the data from google trend with different keywords continuously.But it can't work.
It seems that the "pytrend.trend" can't work. But I have referred to the introduction of pytrends package and
can't find where the mistakes are. Can anyone help me?
Code:
# -*- coding: utf-8 -*-
import time
import codecs
import random
import glob  
from pytrends.request import TrendReq

google_username = ""
google_password = ""

f = open('stocks_tw.txt', 'r')
stocks_no_name = []
for line in f.readlines():
    data = line.split('\t')
    stock_no = data[0].strip()
    stock_name = data[1].strip()
    stocks_no_name.append([stock_no, stock_name])
f.close()


files=glob.glob('*.csv')  
downloaded_files = [fd.title().lower()[0:4] for fd in files]

stocks_no_name_new = [] 
for stock_no_name in stocks_no_name:
    if not stock_no_name[0] in downloaded_files:
        stocks_no_name_new.append(stock_no_name)
stocks_no_name = stocks_no_name_new       
    
print len(stocks_no_name)

# connect to Google
pytrend = TrendReq(google_username, google_password, custom_useragent='My Pytrends Script')

while stocks_no_name:
    stock_index = random.randint(0,len(stocks_no_name)-1)
    stock_no_name = stocks_no_name[stock_index]
    stock_no = stock_no_name[0]
    stock_name = stock_no_name[1]
    print stock_no, stock_name

    try:
        one_stock_data = []    
        trend_payload = {'q': stock_name, 'date': '2013-12-29 2016-12-31', 'geo': 'TW','tz': 'Etc/GMT+8'}
        # trend
        trend = pytrend.trend(trend_payload)
        time.sleep(random.randint(120, 360))
    
        table = trend['table']
        rows = table['rows']
        for i in range(len(rows)):
            row_data = []
            for j in range(len(rows[0]['c'])):
                row_data.append(rows[i]['c'][j]['v'])
            one_stock_data.append(row_data) 
                
        # output one_stock_data to a file
        filename = unicode(stock_no, errors='ignore') + '.csv'
        outfile = codecs.open(filename, "wb", "utf-8")
        for i in range(len(one_stock_data)):
            one_stock_data_str =  str(one_stock_data[i][0]) + ", " + str(one_stock_data[i][1])
            if i != len(one_stock_data) - 1:
                one_stock_data_str =  one_stock_data_str + "\r\n"
            outfile.write(one_stock_data_str)
        outfile.close()
        
        stocks_no_name.pop(stock_index)
    
    except:
        time.sleep(random.randint(120, 360))
        continue