I just recently joined an AI hackathon where we took on the challenging task of trying to recognize fake news. Early on I worked on automatically scraping news articles from various different news sites. I was surprised of how easy this was to implement using a really nice Python library called Newspaper.

Web

Note: The code repository contains improvements that are not inlucded in this tutorial. Please do read through to understand how the code works, but make sure to also have a look at the source code afterwards.

In this article, we'll see how easy it is to perform web scraping using a headless browser. Specifically, we'll see a Puppeteer tutorial that goes through a few examples of how to control Google Chrome to take screenshots and gather structured data. Learn how to scrape news articles from Yahoo! News using python. News is a news aggregations service and in this tutorial you'll learn how to create a.

Web Scraping News Articles In Python

Fresh tech news gathered from around the web. Sites such as slashdot, gizmodo, engadget and hacker news are represented. In this article, we'll see how easy it is to perform web scraping using a headless browser. Specifically, we'll see a Puppeteer tutorial that goes through a few examples of how to control Google Chrome to take screenshots and gather structured data. In this in depth tutorial series, you will learn how to use Selenium + Python to crawl and interact with almost any websites. Selenium is a Web Browser Automation Tool originally designed to.

I haven’t really worked very much with Python before, and never realized how many great libraries that are available for Python users.Some are so well made and feature rich that with an interface, could work as standalone products.There are few programming languages/frameworks that can compete with the resources available for Python users.

We wanted to gather large amounts of news articles to train out network so that it could distinguish real news from fake news.It was important to have the data in a tidy format so that it would be easy for us to work with.To automate the process, I created a scraping script with the help of Newspaper.Go take and take look at the library, it can do so much more than just scraping articles on the web!I also use Feedparser to read RSS-feeds, as I did not realize before later that Newspaper also has this feature already built in.The script relies mainly on scraping articles from the RSS-feed of the website when they have an RSS-feed is available.As a fall back option Newspapers’ automatic article scraper is used for sites where I could not find any RSS-feed.I decided to scrape from the RSS-feed first because the data was much more consistent when gathered through the RSS-feed.Especially the publish date/time of the article would often be missing when using the automatic article scraper.Since the publish date was important for our solution I put extra focus on trying get this included in the dataset.

We start by importing some libraries. We also import mktime and datetime that will be used to format various date forms on to the same format.The download limit for each website is set here to 4, but can of course be higher.We also initialize a data object that we will store our scraped data in.

Next thing we will do is to create a file called NewsPapers.json where we can easily add and remove websites/newspapers we want the script to scrape.This file will be a JSON file on the format like this:

It a good mix of websites you could say…

So we open this json file in our python script:

Note that the naming is a little inconsistent (e.g. companies/newspaper/website is all the same), it was created on a hackathon with limited time spent thinking about variable names.

We will break this into parts and see what is going on.

What we do here is to iterate through our imported JSON-file and checking whether a rss key is provided for each company website.If this value exists, we’ll use FeedParser to load the RSS-feed of the given website.We start building the structure for the data we want to gather by constructing a dictionary newsPaper.

The variable d contains a list of links to articles taken from the RSS-feed that we will loop through.To get consistent data a check is done to see if the entry has a publish date. If it does not have one the entry is discarded.An article dictionary is created to store data for each article.To get the publish date, we extract the published_parsed value from the entry and do some formatting to get it on the same form as dates given by the Newspaper library (Note: I can imagine are better ways to do this).

While we have gone through the RSS-feed, we have not actually scraped the articles yet.To do this we use the Newspaper library to scrape the content of the links we got from the RSS-feed.We put this into a try block just in case the loading fails, ensuring that the script continues without crashing.If anything weird happens, the script will dump some text and then the continue will jump the script to the next loop.

Web Scraping News Articles In Python

If everything works fine we will store the title and text to our article object and then add this to the list of articles in the newsPaper dictionary.

Python Web Scraping Tools

Now, not every site has a RSS-feed anymore as it is to some degree, a dying technology (Did I just say that?).I wanted to get all articles via RSS because the data would be much more consistent, but for those websites that do not have one we need a backup.

The else-block is pretty similar to the if-block, the only difference is that the articles are scraped directly from the frontpage of the website.

This builds the list of articles found on the frontpage of the website.

Web Scraping News Articles In Python Language

Because the Newspaper library often failed to extract the publishing time of the article, I added a part to check if mulitple articles in a row were missing a publish time then the script would just skip the whole newspaper.

Finally, the data is stored to each individual company (website) and the data object is saved to file as JSON.

That was a quick overview of how to easily scrape any news site you want.If you have any questions regarding the code, please do not hesitate to leave an issue on the GitHub project page. :)

Web Scraping News Articles In Python Programming

Please enable JavaScript to view the comments powered by Disqus.