Guest User

Untitled

a guest
Dec 22nd, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. # Requires pandas. For Windows users, I recommend installing the Anaconda Python distirbution.
  2. # Requires the pytrends library. To install, run "pip install pytrends".
  3. from pytrends.pyGTrends import pyGTrends
  4. import time
  5. import os
  6. from random import randint
  7. import pandas as pd
  8.  
  9. # Add your Gmail username to the google_username variable and your Gmail password to the google_password variable.
  10. google_username = ""
  11. google_password = ""
  12. connector = pyGTrends(google_username, google_password)
  13.  
  14. # This script downloads a series of CSV files from Google Trends. Please specify a filepath for where you'd like these files to be stored in the below variable.
  15. path = ""
  16.  
  17. # Specify the filename of a CSV with a list of keywords in the variable, keyordcsv. The CSV should be one column, with header equal to Keywords (case sensitive).
  18. keywordcsv = "keywords.csv"
  19. keywords = pd.read_csv(keywordcsv)
  20.  
  21. # Downloads and Calculate Slope:
  22. keywordlist = pd.DataFrame(columns=["keyword","slope"])
  23. for index, row in keywords.iterrows():
  24. print("Downloading Keyword #" + str(index))
  25. payload = {'geo': 'US', 'q': [row[0]]}
  26. connector.request_report(payload)
  27. time.sleep(randint(5, 10))
  28. connector.save_csv(path, str(index))
  29. csvname = str(index)+".csv"
  30. trenddata = pd.read_csv(csvname, skiprows=4, names=['date', 'values'])
  31. keyword = trenddata['values'].loc[[0]][0]
  32. trenddata = trenddata.ix[1:]
  33. trenddata['keyword'] = keyword
  34. trenddata.rename(columns={'values': 'trends'}, inplace=True)
  35. trenddata['trends'] = pd.to_numeric(trenddata['trends'], errors='coerce')
  36. trenddata['date'] = trenddata['date'].str.extract('(^[0-9]{4}\-[0-9]{2}\-[0-9]{2}) \-.*')
  37. trenddata = trenddata.dropna()
  38. trenddata['date'] = pd.to_datetime(trenddata['date'])
  39. trenddata['year'] = pd.DatetimeIndex(trenddata['date']).year
  40. trenddata['month'] = pd.DatetimeIndex(trenddata['date']).month
  41. trenddata['day'] = pd.DatetimeIndex(trenddata['date']).day
  42.  
  43. maxyear = trenddata['year'].max()
  44. grouped = trenddata.groupby(['year']).mean()
  45.  
  46. def slope_formula(xone, yone, xtwo, ytwo):
  47. return (ytwo-yone)/(xtwo-xone)
  48.  
  49. maxyear = trenddata['year'].max()
  50. grouped = trenddata.groupby(['year']).mean()
  51. slope = slope_formula(1,float(grouped.loc[grouped.index==maxyear-2]['trends']),
  52. 2,float(grouped.loc[grouped.index==maxyear-1]['trends']))
  53. keywordlist = keywordlist.append({'keyword':keyword,'slope':slope}, ignore_index=True)
  54. os.remove(csvname)
  55.  
  56. # Specify a csv filename to output the slope values.
  57. keywordlist.to_csv("trends_slope.csv", sep=",", encoding="utf-8", index=False)
  58.  
  59. print("Slope calculation and CSV export complete.")
Add Comment
Please, Sign In to add comment