The Algorithms logo
The Algorithms
À proposFaire un don

Scrap Newsfrom India Today

H
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from bs4 import BeautifulSoup\n",
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def make_soup(url):\n",
    "    return BeautifulSoup(requests.get(url).text, 'html.parser')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "url = 'https://www.indiatoday.in/top-stories'\n",
    "indiatoday = 'https://www.indiatoday.in'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "top_stories = make_soup(url).find_all('div',{'class':'catagory-listing'})\n",
    "articles_list = []\n",
    "for story in top_stories:\n",
    "    image = story.find('img')['src']\n",
    "    title = story.find('a').text\n",
    "    story_soup = make_soup(indiatoday + story.find('a')['href'])\n",
    "    brief = story.find('p').text\n",
    "    \n",
    "    article = []\n",
    "    for description in story_soup.find_all('div',{'class':'description'}): \n",
    "        for paragraph in description.find_all('p'):\n",
    "            article.append(paragraph.text)\n",
    "\n",
    "    articles_list.append([title, brief, article, image])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame(articles_list, columns=['Title', 'Brief Intro', 'Paragraph', 'Image Url'])"
   ]
  }
 ],
 "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
À propos de cet Algorithme
import numpy as np
import pandas as pd
from bs4 import BeautifulSoup
import requests
def make_soup(url):
    return BeautifulSoup(requests.get(url).text, 'html.parser')
url = 'https://www.indiatoday.in/top-stories'
indiatoday = 'https://www.indiatoday.in'
top_stories = make_soup(url).find_all('div',{'class':'catagory-listing'})
articles_list = []
for story in top_stories:
    image = story.find('img')['src']
    title = story.find('a').text
    story_soup = make_soup(indiatoday + story.find('a')['href'])
    brief = story.find('p').text
    
    article = []
    for description in story_soup.find_all('div',{'class':'description'}): 
        for paragraph in description.find_all('p'):
            article.append(paragraph.text)

    articles_list.append([title, brief, article, image])
df = pd.DataFrame(articles_list, columns=['Title', 'Brief Intro', 'Paragraph', 'Image Url'])