Skip to content
This repository has been archived by the owner on Jun 2, 2023. It is now read-only.

Commit

Permalink
Merge pull request #3 from pedromlsreis/dates
Browse files Browse the repository at this point in the history
Get dates from a given chatfile.html
  • Loading branch information
pedromlsreis authored Jan 31, 2019
2 parents ba4cb9f + 2779a44 commit 3a414c1
Showing 1 changed file with 144 additions and 0 deletions.
144 changes: 144 additions & 0 deletions scraping-messenger.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import csv\n",
"from bs4 import BeautifulSoup\n",
"import re\n",
"from datetime import datetime\n",
"import pandas as pd\n",
"\n",
"\n",
"# import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def get_dates(filename):\n",
" #Facebook Messenger date format: Jun 8, 2013, 10:50 PM\n",
" date_pattern = re.compile(r'[A-Z]{1}[a-z]{2}\\s\\d+[,]\\s\\d{4}[,]\\s\\d{1,2}[:]\\d{2}\\s[AM|PM]')\n",
" with open(filename, mode='r', encoding='UTF-8', errors='ignore') as fp:\n",
" soup = BeautifulSoup(fp, \"lxml\", from_encoding='utf-8')\n",
" soup.find('head').decompose() #delete the <head>, useless for this project\n",
" soup.find('div', id=\"bluebarRoot\").decompose() #delete the blue header\n",
" dates = soup.find_all(text=date_pattern)\n",
" csvfile = csv.writer(open(\"chatdata.csv\", \"w\", encoding='UTF-8'))\n",
" csvfile.writerow([\"date\"])\n",
" for date in dates:\n",
" data = datetime.strptime(date,'%b %d, %Y, %I:%M %p')\n",
" csvfile.writerow([data])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"get_dates(r\"\\Path\\to\\File\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>date</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2018-12-22 14:23:00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2018-12-21 02:35:00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2018-12-21 02:35:00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2018-12-21 02:35:00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2018-12-21 02:35:00</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" date\n",
"0 2018-12-22 14:23:00\n",
"1 2018-12-21 02:35:00\n",
"2 2018-12-21 02:35:00\n",
"3 2018-12-21 02:35:00\n",
"4 2018-12-21 02:35:00"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.read_csv('chatdata.csv')\n",
"df.head()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 3a414c1

Please sign in to comment.