Resource Recommendation

前段时间做项目需要爬Facebook,但因为疫情原因官方的个人Graph API暂停申请权限,抓耳挠腮之际只能奔向万能的GitHub找资源。多多少少试了好多包,把个人觉得比较好的罗列在下面,仅供个人学习和交流,不用于商业用途。

  1. 在线 Facebook主页基本信息(公开的地址、电话、邮箱、营业时间等等)爬取工具, 快速便捷,有免费试用版。https://phantombuster.com/automations/facebook/8369/facebook-profile-scraper
  2. 来自GitHub,试了下爬取个人主页的相关帖子、视频等等还是很强大的,需要有效的credentials(注册邮箱和密码)。 https://github.com/harismuneer/Ultimate-Facebook-Scraper
  3. 来自GitHub,可以爬取公共主页所有帖子、对应时间、转赞评数目、帖子ID等,不需要credentials,是我找到的少数几个能爬公共主页的有效代码,可惜评论的具体内容无法爬取。https://github.com/kevinzg/facebook-scraper

Practical Usage

最终选择上述第三种方法来爬取目标公司Facebook公共主页的所有帖子并输出xlsx数据:

import re
import time
import datetime
import pandas as pd
import numpy as np
from Facebook_Scraper.facebook_scraper import get_posts
from Facebook_Scraper.facebook_scraper import fetch_share_and_reactionsdef facebook_scrap():# The data type of incorporation date and dissolution date is timestamp, we'll convert them into string containing only date.data = pd.read_excel('../data/dataset.xlsx',converters={'Date of Establishment_legal':str,'Dissolved_legal':str})# Column 'Date of Establishment_legal' contains the company's incorporation date, column 'Dissolved_legal' contains the company's dissolution date, and column 'Facebook' contains the link of the Facebook public page of the company if any. # We only extract companies with Facebook linksdata = data[data['Facebook'].notna()]  data['Date of Establishment_legal'] = data['Date of Establishment_legal'].apply(lambda x: x[0:10])data['Dissolved_legal'] = data['Dissolved_legal'].apply(lambda x: x[0:10] if type(x)==str else(x))# The input of Facebook scraping code should be its account name, so we need to extract account name from the linklinks = data['Facebook'].to_list()     account = [0 for _ in range(data.shape[0])]pattern = re.compile('https://www.facebook.com/([a-zA-Z0-9.]+)')for i in range(len(links)):try:name = re.findall(pattern, links[i])[0]account[i] = nameexcept:account[i] = 0posts_data = pd.DataFrame({"post_id":"","text":"","post_text":"","shared_text":"","time":"","image":"","likes":"","comments":"",\"shares":"","post_url":"","link":""},index=["0"])abbreviation = data['Company name_abbreviation'].to_list()incorporation_date = data['Date of Establishment_legal'].to_list()dissolution_date = data['Dissolved_legal'].to_list()# Starting to scrap postsfor i in range(0,len(account)):cnt = 0#There are about 2 posts per page, and pages=4000 should be enough for us to scrap all the Facebook posts since the account was created.for post in get_posts(account = account[i], pages=4000):  cnt += 1more_info_post = fetch_share_and_reactions(post)more_info_post['Company name_abbreviation'] = abbreviation[i]more_info_post['account'] = account[i]more_info_post['incorporation_date'] = incorporation_date[i]more_info_post['dissolution_date'] = dissolution_date[i]df = pd.DataFrame(more_info_post,index=["0"])posts_data = posts_data.append(df,ignore_index=True,sort=False)print(account[i],cnt,' posts are scraped.')useful_columns = ['post_id','text','shared_text','time','image','likes','comments','shares','post_url','link',\'Company name_abbreviation','account','incorporation_date','dissolution_date']posts_data = pd.DataFrame(posts_data, columns=useful_columns)posts_data = posts_data.drop([0])posts_data.to_excel('../data/all_facebook_posts.xlsx',index=False)return posts_data

Python爬取Facebook公共主页帖子相关推荐

  1. 简书爬ajax接口获取csrf,Python爬取简书主页信息

    主要学习如何通过抓包工具分析简书的Ajax加载,有时间再写一个Multithread proxy spider提升效率. 1. 关键点: 使用单线程爬取,未登录,爬取简书主页Ajax加载的内容.主要有 ...

  2. python爬取贴吧所有帖子-python爬取贴吧帖子

    一.介绍 我们常遇到一些很长的贴吧连载帖子想存到本地再看 此文就是运用python爬取指定百度贴吧的帖子并存到本地满足需求 环境:python2.7 目标网页:[长篇连载]剑网3的正史和野史--从头开 ...

  3. python爬取贴吧所有帖子-Python爬虫实例(一)爬取百度贴吧帖子中的图片

    程序功能说明:爬取百度贴吧帖子中的图片,用户输入贴吧名称和要爬取的起始和终止页数即可进行爬取. 思路分析: 一.指定贴吧url的获取 例如我们进入秦时明月吧,提取并分析其有效url如下 ?后面为查询字 ...

  4. python爬取虎扑论坛帖子数据

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取 python免费学习资 ...

  5. Python:爬取FaceBook用户头像

    博客迁移 个人博客站点,欢迎访问,www.jiingfengji.tech 本文地址 Python爬取源码 本文介绍的爬取方法是基于已经有一些用户的sns_id了,然后通过头像链接进行头像下载. 以下 ...

  6. python爬取贴吧所有帖子-通过python爬取贴吧数据并保存为word

    前言 Python是一种跨平台的计算机程序设计语言.是一种面向对象的动态类型语言,最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越来越多被用于独立的.大型项目的开发 ...

  7. python爬取贴吧所有帖子-Python爬虫爬取百度贴吧的帖子

    同样是参考网上教程,编写爬取贴吧帖子的内容,同时把爬取的帖子保存到本地文档: #!/usr/bin/python #_*_coding:utf-8_*_ import urllib import ur ...

  8. python爬取贴吧所有帖子-Python实现的爬取百度贴吧图片功能完整示例

    本文实例讲述了Python实现的爬取百度贴吧图片功能.分享给大家供大家参考,具体如下: #coding:utf-8 import requests import urllib2 import urll ...

  9. python爬取贴吧所有帖子-python 爬虫爬取百度贴吧,获取海量信息

    需要用到的库:requests,re,xpath 首先打开随便一个贴吧:贴吧首页 通过观察发现每一个帖子的链接是这样的:帖子链接 我们只需要获取后面灰色部分就可以了,点击f12 按ctrl+f 找到链 ...

  10. python爬虫(13)爬取百度贴吧帖子

    爬取百度贴吧帖子 一开始只是在网上看到别人写的爬取帖子的文章,然后自己就忍不住手痒自己锻炼一下, 然后照着别人的写完,发现不太过瘾, 毕竟只是获取单个帖子的内容,感觉内容稍显单薄,然后自己重新做了修改 ...

最新文章

  1. abap中的弹出窗口函数
  2. typedef 函数指针的用法
  3. Spring与Rails的jQuery UJS
  4. 列注释_机器学习 Pandas 03:基础 前16题 ( 带答案、注释 )
  5. python opencv 内存泄露_python - OpenCV Python裁剪图像 - 堆栈内存溢出
  6. 最新JS验证手机号码(2021)
  7. uploadify php使用,如何在ThinkPHP中使用Uploadify对图片进行上传
  8. oracle报错ora-01033解决办法
  9. 简单的使用EA进行需求管理
  10. 《机器学习算法竞赛实践》学习笔记(1)神经网络
  11. 一文理解二元logistic回归
  12. Ubuntu 2004 鼠标可以移动但是点击无响应 排查流程
  13. 查看和调试core文件
  14. 分享卖货小程序制作方法_怎么在微信上做小程序卖货
  15. linux系统工程师----2.linux中内核级加强型火墙的管理
  16. C# 简单的操作其他程序
  17. SQL Pass北京将举办第一次线下活动,欢迎大家报名
  18. 人事管理系统多用于哪方面的工作?
  19. 使用c++,类实现求圆柱体体积
  20. Unity3D Global Volume没有效果的解决办法

热门文章

  1. SOLIDWORKS参数化设计方式几多种,你都用恰当了吗?
  2. Sphinx速成指南
  3. DRILLNET 2.0------第十三章 尾管固井扭矩/摩阻模型
  4. Python图形界面编程
  5. 几个免费下载原版图书的网站
  6. Python实现猜拳游戏
  7. led背光板的作用是什么呢?
  8. 蒙氏素材1-1000珠链标签蒙特梭利教具
  9. 【MATLAB】三维绘图 三维数据插值
  10. 启用计算机的fn键,怎么关闭手提电脑上的FN键功能?我的是联想