作者earlywinter (earlywinter)
看板Python
标题[问题]写爬FB粉丝专业资讯时遇到问题
我在写爬FB资料爬完後,想把爬的资料写成csv档,但我用jupyter看只有标题,
没有我抓的资料,很确定他有再爬资料。
还有如果我要把档案写在D槽,我该如何写? 谢谢各位大大了
下面是我写的程式码
import requests
import pandas as pd
while 'paging' in res.json():
for index, information in enumerate(res.json()['data']):
print('正在爬取第{}页,第{}篇文章'.format(page, index + 1))
if 'message' in information:
res_post =
requests.get('
https://graph.facebook.com/v2.9/{}/likes?limit=1000&access_token={}'.format(information['id'],
token))
try:
if 'next' not in res_post.json()['paging']:
for likes in res_post.json()['data']:
information_list.append([information['id'],
information['message'], parse(information['created_time']).date(),
likes['id'], likes['name']])
elif 'next' in res_post.json()['paging']:
while 'paging' in res_post.json():
for likes in res_post.json()['data']:
information_list.append([information['id'],
information['message'], parse(information['created_time']).date(),
likes['id'], likes['name']])
if 'next' in res_post.json()['paging']:
res_post =
requests.get(res_post.json()['paging']['next'])
else:
break
except:
information_list.append([information['id'],
information['message'], parse(information['created_time']).date(), "NO",
"NO"])
if 'next' in res.json()['paging']:
res = requests.get(res.json()['paging']['next'])
page += 1
else:
break
print('爬取结束!')
df = pd.DataFrame(information_list, columns=['发文ID', '发文内容', '发文时间
', '按赞ID', '按赞名字'])
df.to_csv('台湾资料科学年会.csv', index=False)
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 1.163.99.217
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1520767195.A.5DD.html
※ 编辑: earlywinter (1.163.99.217), 03/11/2018 22:43:22
1F:→ HenryLiKing: 干嘛删掉留言啊 03/11 22:44
2F:→ earlywinter: 抱歉,我是刚来用ptt的,因为我是直接换程式码想说下 03/12 02:49
3F:→ earlywinter: 面留言不要混淆到就删了 03/12 02:50
4F:→ Jyery: 删留言在每个看板都是大忌耶 03/12 10:43
5F:→ earlywinter: sorry..学到了 03/12 11:47
6F:→ sky800507: 这程式码好眼熟XD 03/12 20:29
7F:→ earlywinter: 是我用网路上某大大的 03/14 15:55