Web Scraping Github



I’ve recently had to perform some web scraping from a site that required login.It wasn’t very straight forward as I expected so I’ve decided to write a tutorial for it.

Scrapy is a fast high-level web crawling and web scraping framework, used to crawl websites and extract structured data from their pages. It can be used for a wide range of purposes, from data mining to monitoring and automated testing. Scrapy is maintained by Zyte (formerly Scrapinghub) and many other contributors. I’ve recently had to perform some web scraping from a site that required login. It wasn’t very straight forward as I expected so I’ve decided to write a tutorial for it. For this tutorial we will scrape a list of projects from our bitbucket account. The code from this tutorial can be found on my Github. We will perform the following steps. A few weeks back I was faced with a challenge that was basically to use webscraping to get all the files of a GitHub repository and group them by extension and sum the size for those files of that particular extension. The important thing about this is that we SHOULD NOT use Github. Web scraping refers to the action of extracting data from a web page using a computer program, in this case our computer program will be R. Other popular command line interfaces that can perform similar actions are wget and curl. GitHub Gist: instantly share code, notes, and snippets. GitHub Gist: instantly share code, notes, and snippets. Skip to content. JayLee18 / 5 - Collecting Data Using Web Scraping.ipynb. Created Apr 23, 2021. Star 0 Fork 0; Star Code Revisions 1. What would you like to do? Embed Embed this gist in your website.

For this tutorial we will scrape a list of projects from our bitbucket account.

The code from this tutorial can be found on my Github.

We will perform the following steps:

  1. Extract the details that we need for the login
  2. Perform login to the site
  3. Scrape the required data

For this tutorial, I’ve used the following packages (can be found in the requirements.txt):

Open the login page

Go to the following page “bitbucket.org/account/signin” .You will see the following page (perform logout in case you’re already logged in)

Check the details that we need to extract in order to login

In this section we will build a dictionary that will hold our details for performing login:

  1. Right click on the “Username or email” field and select “inspect element”. We will use the value of the “name” attribue for this input which is “username”. “username” will be the key and our user name / email will be the value (on other sites this might be “email”, “user_name”, “login”, etc.).
  2. Right click on the “Password” field and select “inspect element”. In the script we will need to use the value of the “name” attribue for this input which is “password”. “password” will be the key in the dictionary and our password will be the value (on other sites this might be “user_password”, “login_password”, “pwd”, etc.).
  3. In the page source, search for a hidden input tag called “csrfmiddlewaretoken”. “csrfmiddlewaretoken” will be the key and value will be the hidden input value (on other sites this might be a hidden input with the name “csrf_token”, “authentication_token”, etc.). For example “Vy00PE3Ra6aISwKBrPn72SFml00IcUV8”.

We will end up with a dict that will look like this:

Keep in mind that this is the specific case for this site. While this login form is simple, other sites might require us to check the request log of the browser and find the relevant keys and values that we should use for the login step.

For this script we will only need to import the following:

First, we would like to create our session object. This object will allow us to persist the login session across all our requests.

Scraping

Second, we would like to extract the csrf token from the web page, this token is used during login.For this example we are using lxml and xpath, we could have used regular expression or any other method that will extract this data.

** More about xpath and lxml can be found here.

Next, we would like to perform the login phase.In this phase, we send a POST request to the login url. We use the payload that we created in the previous step as the data.We also use a header for the request and add a referer key to it for the same url.

Now, that we were able to successfully login, we will perform the actual scraping from bitbucket dashboard page

In order to test this, let’s scrape the list of projects from the bitbucket dashboard page.Again, we will use xpath to find the target elements and print out the results. If everything went OK, the output should be the list of buckets / project that are in your bitbucket account.

You can also validate the requests results by checking the returned status code from each request.It won’t always let you know that the login phase was successful but it can be used as an indicator.

for example:

That’s it.

Full code sample can be found on Github.

About me

Hi, my name is Keith and I am a YouTuber who focuses on content related to programming, data science, and machine learning!

Here is a link to my channel: youtube.com/kgmit

I grew up in the great state of New Hampshire here in the USA. From an early age I always loved math. Around my senior year of high school, my brother first introduced me to programming. I found it a creative way to apply the same type of logical thinking skills that I enjoyed with math. This influenced me to study computer science in college and ultimately create a YouTube channel to share some things that I have learned along the way.

Hobbies

Believe it or not, I don't code 24/7. I love doing all sorts of active things. I like to play ice hockey & table tennis as well as run, hike, skateboard, and snowboard. In addition to sports, I am a board game enthusiast. The two that I've been playing the most recently are Settlers of Catan and Othello.

Fun Facts

Github web template
  • Owned my dream car in high school 1
  • Middle name is Ronald
  • Never had been on a plane until college
  • Dunkin Donuts coffee is better than Starbucks
  • A favorite book series of mine is Ender's Game
  • Current video game of choice is Rocket League
  • The band that I've seen the most times live is the Zac Brown Band

Social Media

I encourage you to check out my content on all social media platforms

Photos

Here are a few photos from a trip to italy I took last year

Table

My MIT hockey stats :)
STeamLeagueGPGATPPIM+/-POSTGPGATPPIM+/-
2014-15 MIT (Mass. Inst. of Tech.) ACHA II 17391220 |
2015-16 MIT (Mass. Inst. of Tech.) ACHA II 91122 |
2016-17 MIT (Mass. Inst. of Tech.) ACHA II 12551080 |
2017-18 Did not play |
2018-19 MIT (Mass. Inst. of Tech.) ACHA III 8510158 |

Mystery Message Challenge!

If you scrape the links below grabbing the <p> tag with, you'll discover a secret message :)

Footnotes

Python Web Scraping Github