ranking_report.py 944 B

12345678910111213141516171819202122232425262728293031
  1. import pandas as pd
  2. import dataset
  3. import pymysql
  4. pymysql.install_as_MySQLdb()
  5. name='好晴天'
  6. date = '0731'
  7. def day():
  8. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
  9. cursor = db.query(f'select term, domain from seo.selected_kw where client ="{name}"')
  10. kw_lst = []
  11. for c in cursor:
  12. kw_lst.append(c['term'])
  13. df = pd.DataFrame(kw_lst,columns=['kw'])
  14. rank_lst = []
  15. for i in kw_lst:
  16. cursor_general = db.query(f'select kw,ranking,url from seo.google_bank where kw = "{i}" order by dt desc limit 1')
  17. for c in cursor_general:
  18. rank_lst.append([c['kw'],c['ranking'],c['url']])
  19. db.close()
  20. df1 = pd.DataFrame(rank_lst,columns=['kw','ranking','url'])
  21. df_result = pd.merge(df, df1, on='kw', how='outer').fillna(101)
  22. df_result.to_csv(f"/Users/mac/Documents/wk/{date}{name}關鍵字排名.csv", index=False)
  23. print('完成')
  24. day()