123456789101112131415161718192021222324252627 |
- import pandas as pd
- import dataset
- import pymysql
- pymysql.install_as_MySQLdb()
- name='辜琪鈞'
- date = '1128'
- def day():
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
- cursor = db.query(f'select term, domain from seo.selected_kw where client ="{name}"')
- kw_lst = []
- for c in cursor:
- kw_lst.append(c['term'])
- df = pd.DataFrame(kw_lst,columns=['kw'])
- rank_lst = []
- for i in kw_lst:
- cursor_general = db.query(f'select kw,ranking from seo.general_log where kw = "{i}" order by dt desc limit 1')
- for c in cursor_general:
- rank_lst.append([c['kw'],c['ranking']])
- db.close()
- df1 = pd.DataFrame(rank_lst,columns=['kw','ranking'])
- df_result = pd.merge(df, df1, on='kw', how='outer').fillna(0)
- df_result.to_csv(f"C:\/Users\/s1301\/Documents\/{date}{name}關鍵字排名.csv", index=False)
|