Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions projects/News website scraper/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Financial-news-scraper
A scraper made using beautiful soup 4 in python. Tailor made for extracting news from moneycontrol.com. Issue pull request for different scrapers.

__The main page to start scraping from: https://www.moneycontrol.com/news/technical-call-221.html__
![](images/home.JPG)

__The program scrapes news from next pages too by extracting website link in these buttons__
![](images/nextpage.JPG)

__Resulting JSON file includes heading, date and image link, indexed by page number__
![](images/result.JPG)
5 changes: 5 additions & 0 deletions projects/News website scraper/images/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
home.jpg - main news page

nextpage.jpg - links to next pages

result.jpg - Snapshot of result json file
Binary file added projects/News website scraper/images/home.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added projects/News website scraper/images/nextpage.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added projects/News website scraper/images/result.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions projects/News website scraper/moneycontrol_scrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import re
import json
import requests
import datetime
from tqdm import tqdm
from bs4 import BeautifulSoup
from collections import defaultdict

submission = defaultdict(list)
#main url
src_url = 'https://www.moneycontrol.com/news/technical-call-221.html'

#get next page links and call scrap() on each link
def setup(url):
nextlinks = []
src_page = requests.get(url).text
src = BeautifulSoup(src_page, 'lxml')

#ignore <a> with void js as href
anchors = src.find("div", attrs={"class": "pagenation"}).findAll(
'a', {'href': re.compile('^((?!void).)*$')})
nextlinks = [i.attrs['href'] for i in anchors]
for idx, link in enumerate(tqdm(nextlinks)):
scrap('https://www.moneycontrol.com'+link, idx)

#scraps passed page url
def scrap(url, idx):
src_page = requests.get(url).text
src = BeautifulSoup(src_page, 'lxml')

span = src.find("ul", {"id": "cagetory"}).findAll('span')
img = src.find("ul", {"id": "cagetory"}).findAll('img')

#<img> has alt text attr set as heading of news, therefore get img link and heading from same tag
imgs = [i.attrs['src'] for i in img]
titles = [i.attrs['alt'] for i in img]
date = [i.get_text() for i in span]

#list of dicts as values and indexed by page number
submission[str(idx)].append({'title': titles})
submission[str(idx)].append({'date': date})
submission[str(idx)].append({'img_src': imgs})

#save data as json named by current date
def json_dump(data):
date = datetime.date.today().strftime("%B %d, %Y")
with open('moneycontrol_'+str(date)+'.json', 'w') as outfile:
json.dump(submission, outfile)

setup(src_url)
json_dump(submission)