RSS FeedParser for Medium
Welcome to the third blog in the FeedParser tutorial series. In the previous article we learned how to install feedparser on our machines, after which we parsed a basic website and get the title of the first article of the website.In this blog we will be creating a feedparser for medium.
If you haven’t read the previous article, check out the link below:
This is the code that we will be using to build a feedparser for medium.
So let’s get started.
import feedparserfeed_url = "https://aryanirani123.medium.com/feed"blog_feed = feedparser.parse(feed_url)
We are going to start off by importing the feedparser module, followed by declaring the medium link that we are going to parse. You can use the same link while you learn.
https://aryanirani123.medium.com/feed
After importing feedparser and declaring the link to be parsed, we are going to parse the link by using the feedparser.parse() link followed by passing the link as a parameter.
#Gets the title of the websitewebsite_title = blog_feed.feed.titleprint(website_title)
Now we are going to be getting the title of the website. To do that we are going to be using the blog_feed.feed.title command that will get the title of the website. In this part of the code, the blog_feed variable contains all the data of the website. This is the variable that was created when we parsed the website.
Lets go ahead and run this code.
On running the code you can see we have got the title of the website successfully.
content = blog_feed.entries
print(blog_feed.entries[0].title)
Next we are going to get the entries on the website. We are going to be getting all the blogs that are there on the website. To do that we are going to be using the entries command that will all the blog details.
On getting the details of the blogs, we move forward to getting the title of the first article. In the previous blog we had done something similar where we had got the title of the first article. We will be doing the same here. We will be first getting the first entry on the website(first blog) and then getting the title of the blog.
Lets go ahead and run the code.
Here you can see we have successfully printed the title of the first blog.The next thing we are going to do is print the other details of the blog such as link of the blog, date of published and more.
print(blog_feed.entries[0].title)
print(blog_feed.entries[0].link)
print(blog_feed.entries[0].published)
To print the rest of the details of the article,I am going to be doing the same by using the blog_feed.entries function but going to change the variable after the dot. To get the link we are using .link and to get the date of publishing, we are using .published.
Lets go ahead and run the code.
On running the code, you can see that the title, link and the date of publishing has successfully been printed.
Now that we have interacted with medium by getting all the details of one blog, lets get the same data for multiple blogs.
import feedparserfeed_url = "https://aryanirani123.medium.com/feed"
blog_feed = feedparser.parse(feed_url)content = blog_feed.entriesfor blog in content:
print(blog.title)
print(blog.link)
print(blog.published)
Here I have started out by importing the feedparser module, followed by declaring the url. Next I have parsed the url using the feedparser.url function and passed the url variable as a parameter.
Next I have got all the entries from the website using the blog_feed.entries function.
Inside the content variable, there are multiple blog posts with different titles, links, date of publishing and more. To print each blog with their details, we are going to be using a for loop to iterate through each of the blogs and print them.
I have started out by opening a for loop and iterate through the content variable and print the blog’s title, link and date of publishing.
Our code is complete, go ahead and run the code.
On clicking run, you can see that all the blogs have been printed with their title, link and date of publishing.
This is all for this blog, I hope you understood how to create a basic feedparser for medium. In the next blog, we will be doing some advanced parsing in medium using the datetime library and get blogs according to specific dates and times.
Feel free to reach out to me if you have any issues/feedback at aryanirani123@gmail.com