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.

Modules Needed:

  • Tweepy:The Python client for the official Twitter API is called Tweepy. Utilise pip to install it by using the command:
  • MongoClient:With the help of the MongoClient class, your program may successfully connect to MongoDB servers. Utilise pip to install it by using the command:
  • Pyshorteners:Pyshorteners are used to programmatically shorten, brand, distribute, or get information from URLs. Install it in the following methods.

Authentication

One must register an App using their Twitter account to retrieve tweets using the Twitter API. For the same, follow these steps:

  • Go to this website, https://apps.twitter.com, and select the 'Create New App' button.
  • Complete the application's information. The callback URL box is optional.
  • You will be routed to the app page after the app has been created.
  • The "Keys and Access Tokens" tab should be opened.
  • Consumer Key, Consumer Secret, Access Token, and Access Token Secret should be copied and pasted into the code below.

Code:

Output:

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!

Explanation:

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.






Latest Courses