{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# HOW WE FEEL ABOUT JOKER"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Scraping IMDB for `JOKER` reviews"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Outputs txt files for corpus creation!!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "import urllib\n",
    "from bs4 import BeautifulSoup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nope\n",
      "nope\n",
      "nope\n",
      "nope\n"
     ]
    }
   ],
   "source": [
    "def get_reviews(rating):\n",
    "    url = \"https://www.imdb.com/title/tt7286456/reviews?sort=helpfulnessScore&dir=desc&ratingFilter=\" + str(rating)\n",
    "    html = urllib.request.urlopen(url).read()\n",
    "    soup = BeautifulSoup(html, 'html.parser')\n",
    "    text = soup.findAll(\"div\", {\"class\": \"imdb-user-review\"})\n",
    "    for num,t in enumerate(text):\n",
    "        scale = t.find(\"span\", {\"class\": \"point-scale\"})\n",
    "        title = t.find(\"a\", {\"class\": \"title\"})\n",
    "        text = t.find(\"div\", {\"class\": \"text show-more__control\"})\n",
    "#         review = title.text + \"==\" + text.text\n",
    "        review = title.text + text.text\n",
    "        try:\n",
    "            print_to_file(scale.previous_sibling.text, review, num)\n",
    "        except:\n",
    "            print('nope')\n",
    "\n",
    "def print_to_file(rating, review, num):\n",
    "#     both = rating + '**' + review\n",
    "    both = review\n",
    "    output_filename = str(rating) + '_jk_v2_' + str(num) + '.txt'\n",
    "    outfile = open(output_filename, 'w')\n",
    "    outfile.write(both)\n",
    "    outfile.close()\n",
    "        \n",
    "for rating in range(1,11):\n",
    "    get_reviews(rating)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
