Scrape the Most Reviewed News and Tweet using Python
Numerous websites will offer up-to-date news about any technology, and the article may be evaluated by the number of reviews it has received. If the news is about cryptocurrency and the articles are taken from cointelegraph.com, we can easily count and store each news item in a MongoDB collection.
One must register an App using their Twitter account to retrieve tweets using the Twitter API. For the same, follow these steps:
Today's date: 2023-07-18 Bitcoin Hits All-Time High -- 10 Ethereum Surges in Value .. 8 New Blockchain Startup Raises $10 Million in Funding .. 6 Impactful News of the Day Bitcoin Hits All-Time High - https://bit.ly/2X1x51V Ethereum Surges in Value - https://bit.ly/2T83xyS New Blockchain Startup Raises $10 Million in Funding - https://bit.ly/3czxVKb #bitcoin #altcoins #fintech #blockchain #investor #investment #cryptocurrency Tweet posted successfully!
The top three news articles with the most reviews are first pulled from a collection based on the current date after establishing a connection to a MongoDB database. It extracts the headlines and stores the relevant URLs using an HTML parser.
The retrieved URLs for each news article are then shortened by the code using the Bitly API. This aids in keeping the tweet inside Twitter's character restriction.
The news headlines and review numbers are added to the abbreviated URLs to create a tweet message. The tweet message also contains hashtags relating to news subjects.
The code then uses the supplied credentials to log in with the Twitter API. The created tweet message is posted to Twitter using the Tweepy library.
The code saves details about the tweeted news, such as the title, review count, shortened URL, and original URL, in a separate MongoDB collection once the tweet is successfully posted. This makes it possible to examine and evaluate the tweeted news in the future.
The code shows a workflow that can be used to find popular news stories, write a brief tweet message, and send it to Twitter while preserving pertinent data for further study.