-
-
Notifications
You must be signed in to change notification settings - Fork 48.7k
Create crawl_hindustan_times_and_get_top_news.py #11645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Mayank-Creater
wants to merge
7
commits into
TheAlgorithms:master
Choose a base branch
from
Mayank-Creater:Crawl-Hindustan-Times-India
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e5fe395
Create crawl_hindustan_times_and_get_top_news.py
Mayank-Creater ea533ae
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 541a64a
Create crawl_hindustan_times_and_get_top_news.py
Mayank-Creater cbef1c6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c8ef30b
Update crawl_hindustan_times_and_get_top_news.py
Mayank-Creater bbba57e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e99d542
Update crawl_hindustan_times_and_get_top_news.py
Mayank-Creater File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
""" | ||
Fetch all the top headlines from Hindustan Times News website with title, link to the news article | ||
Check failure on line 2 in web_programming/crawl_hindustan_times_and_get_top_news.py
|
||
and cover image link. | ||
|
||
The following format is used while displaying the data | ||
|
||
news = { | ||
0: { | ||
"title": <title-of-the-article>, | ||
"link": <link-to-the-news-article>, | ||
"img": <link-to-the-cover-image> | ||
} | ||
} | ||
""" | ||
|
||
import requests | ||
from bs4 import BeautifulSoup | ||
|
||
|
||
def fetch_ht_news(): | ||
|
||
header = { | ||
"Accept": "image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8", | ||
"Sec-GPC": "1", | ||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36", | ||
"sec-ch-ua": '"Not)A;Brand";v="99", "Brave";v="127", "Chromium";v="127"', | ||
"sec-ch-ua-mobile": "?0", | ||
"sec-ch-ua-platform": "Windows", | ||
} | ||
|
||
url = "https://www.hindustantimes.com/" | ||
page_request = requests.get(url, headers=header) | ||
data = page_request.content | ||
soup = BeautifulSoup(data, "html.parser") | ||
|
||
news = {} | ||
|
||
counter = 0 | ||
|
||
for divtag in soup.find_all("div", {"class": "timeAgo"}): | ||
if "liveStory" not in divtag["class"]: | ||
head = divtag.find(class_="hdg3") | ||
title = head.get_text() | ||
link = divtag["data-weburl"] | ||
imgtag = divtag.find("img") | ||
try: | ||
img = imgtag["data-src"] | ||
except Exception: | ||
img = imgtag["src"] | ||
news[counter] = {"title": title, "link": link, "img": img} | ||
counter += 1 | ||
|
||
return news | ||
|
||
|
||
if __name__ == "__main__": | ||
fetch_ht_news() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function:
fetch_ht_news
. If the function does not return a value, please provide the type hint as:def function() -> None: