In [1]:
import os
import requests
import pandas as pd
from datetime import datetime
In [2]:
# 透過 Graph API 觀察文章 ID 與 token
article_id = '1213927345375910'
token = ''
In [3]:
comments = []
pages = 0
url = 'https://graph.facebook.com/v2.11/{}/comments?pretty=0&limit={}&access_token={}'.format(
article_id, 100, token
)
while True:
pages += 1
resp = requests.get(url)
data = resp.json()
comments += data['data']
if 'next' not in data['paging']:
print('EOF')
break
else:
url = data['paging']['next']
print('pages {}'.format(pages))
print('comment length = {}'.format(len(comments)))
In [4]:
df = pd.DataFrame.from_records(comments)
df.head()
Out[4]:
In [5]:
results = os.path.abspath('../results')
if not os.path.exists(results):
os.makedirs(results)
filename = os.path.join(results, '{}.csv'.format(article_id))
df.to_csv(filename, index=False)
print('Save file - {}'.format(filename))