sns_rank_report.py 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. import pandas as pd
  2. import dataset
  3. import pymysql
  4. pymysql.install_as_MySQLdb()
  5. name='理茶'
  6. date = '0216'
  7. def day():
  8. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
  9. cursor = db.query(f'select term, url from seo.sns_kw where client ="{name}"')
  10. kw_lst = []
  11. for c in cursor:
  12. kw_lst.append([c['term'],c['url']])
  13. print(kw_lst)
  14. df = pd.DataFrame(kw_lst,columns=['kw','url'])
  15. rank_lst = []
  16. for i in kw_lst:
  17. # print(i)
  18. kw = i[0]
  19. url = i[1]
  20. cursor_general = db.query(f'select kw,url,ranking from seo.sns_log where kw = "{kw}" and url="{url}" order by dt desc limit 1')
  21. for c in cursor_general:
  22. rank_lst.append([c['kw'],c['url'],c['ranking']])
  23. db.close()
  24. df1 = pd.DataFrame(rank_lst,columns=['kw','url','ranking'])
  25. df_result = pd.merge(df, df1, on=['kw','url'], how='outer').fillna(0)
  26. df_result.to_csv(f"C:\/Users\/s1301\/Documents\/{date}{name}關鍵字排名.csv", index=False)
  27. day()