gsc_test.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import traceback
  2. import copy
  3. import operator
  4. import codecs
  5. import sys
  6. import os
  7. import searchconsole
  8. import dataset
  9. import datetime
  10. import random
  11. db = dataset.connect('mysql://choozmo:pAssw0rd@139.162.121.30:33306/hhh?charset=utf8mb4')
  12. table=db['monitor_kw_raw']
  13. account = searchconsole.authenticate(client_config='c:/keys/service/client_secret.json',credentials='c:/keys/service/credentials.json')
  14. webproperty = account['https://hhh.com.tw/']
  15. allkw=['裝修設計','裝潢','老屋翻新','客廳','玄關設計','臥室','北歐風','廚房','裝潢費用','玄關','廚房收納','裝修','客廳擺設','高雄室內設計','室內設計','小坪數','設計師','廚房設計','客廳裝潢','客廳設計','裝潢設計']
  16. # SELECT keyword,max(dt) FROM hhh.monitor_kw_raw group by keyword;
  17. kw=random.choice(allkw)
  18. #report=webproperty.query.range('2021-01-01', '2020-01-02').dimension('page','query').get()
  19. #kw='裝修設計'
  20. #for kw in all
  21. report=webproperty.query.range('2021-01-01', '2020-05-06').dimension('query').filter('query',kw , 'contains').get()
  22. data=[]
  23. for r in report:
  24. print(r)
  25. query=r[0]
  26. clicks=r[1]
  27. impressions=r[2]
  28. ctr=r[3]
  29. position=r[4]
  30. data.append({'keyword':kw,'query':r[0],'clicks':r[1],'impressions':r[2],'ctr':ctr,'dt':datetime.datetime.now() })
  31. for r in data:
  32. table.insert(r)