️🌑
Try Dark Mode

India's MP Transportation Analysis through Python



Python is really a powerful language and with proper use of it anyone can make beautiful things. After studying Python I was really impressed by its power and to be more specific I really love how we can scrape any website easily with the help of python. Scraping is a process of extracting data from website by their html data. So I learned its basic and started scraping many website.

Recently I thought of creating something big through scraping but I was having no idea what to do. Then I came across with the site of MP transportation and I realized that they got so many data inside there website. The website is very simple, you open the site enter your transport number details and then search it. Then you will get result about your transport vehicle which includes type, color etc.

With python2.7 I created one script to scrape because with python 3.x there were less support to some modules. I decided to go for 'last' search type because with others I was facing some issues (may be site problem). For this I will have to search each input from 0000 - 9999 in short it makes around 10000 requests. We took 4 digits because it requires min 4 characters to enter. So yeah it was this large.

I created one program and started scrapping but then with 0000 input and 'last' type search I found that it scraped successfully and I got 1700+ data. But the problem was that it took 5 minutes to scrape 1 request. This happened because of server delay. It was not my problem but it was server's problem to search this much data from database. After realizing this I did some maths.

If 1 request take = 5 minutes,
then,
10000 requests = 50000 minutes = 833.33 hours = 35 days approx = 1 month 4 days

So in short I need my laptop to run for 1 month and 4 days to run continuously and trust me it's really a bad idea to do so. But is it worth doing it ?

If 1 request is giving approx 1000 data
10000 requests = 10,000,000

So yeah, hypothetically in 35 days I will be able to achieve 10 millions of data.
But still being a programmer we must do stuff as fast as possible and to achieve this one thing is sure that I need some power, memory, security etc. I tried Multiprocessing and multi threading but it was not working as expected

So the solution for this problem was getting your hand on some free servers. So I started searching some free website host company which supports python and thought of deploying my script over there. I tried this in pythonanywhere.com and in Heroku with the help of Flask framework but there was no success. I waited almost 15 days to decide what to do. Later I found one site scrapinghub.com which lets you deploy spider on cloud and rest they will take care of that so I went for it and started learning it.
                           

After that I learned how to use Scrapy and scrapinghub and I created another new program to scrape website with the help of Scrapy spiders. Source code for this is at the end of this page

Result:

Day 1 - 4,092,328 (4 millions of data in 17 hours)

Id1 - Items - 1,134,421 (15 hours)
Id2 - Items - 1,025,282 (17 hours)
Id3 - Items - 983,367    (14 hours)
Id4 - Items - 949,228    (13 hours)

Size - 1.3 GB

Day 2 - 6,498,462 (6.4 millions of data in 17 hours)
(Created 2 more id's to boost my process)

Id1 - Items - 1,241,643 (17 hours)
Id2 - Items - 1000308   (15 hours)
Id3 - Items - 962863     (15 hours)
Id4 - Items - 1052844   (15 hours)
Id5 - Items - 1144686   (16 hours)
Id6 - Items - 1096118   (15 hours)

Size - 2.4 GB

Final Result

Total data collected: 10,590,790
Total size: 3.7 GB
Time consumed: 34 hours

In just 34 hours by scraping we collected 10 millions of data which was estimated earlier. If we tried to do this process in old fashion like in laptop then it would have taken 1 month so we optimized it.
Now what ?

The main question arises is what to do with data ? Which tools to use while analyzing.
Since the size of our JSON files are huge. If we will be able to convert JSON file to database file then it would be really great but doing this will again require loads of time.

From JSON to Database
We can do 5 data per second,
for 10,000,000 = 2,000,000 seconds = 33333 minutes = 555 hours = 23 days.
Now that thing is not possible.

I tried even doing it through SQL script which is much better as compare to the previous script but still it will also take approx 20 days.

So we will use these data in JSON format, load it into python script and then do our maths over there. Loading one file may take approx 10 minutes but time is not an issue. The problem is that loading JSON file in python takes so much of memory. I mean a lot and since we are working on normal laptop then we need to think of something else. To avoid such problem I used ijson module in python. Its really a handy tool which iterates over JSON data rather than loading it all of sudden. But again with this power we need to sacrifice time a little but still its worth it.

Stats

In which state maximum transport is there ?

  1. INDORE - 1625663
  2. BHOPAL - 1023054
  3. JABALPUR - 589875
  4. GWALIOR - 477625
  5. UJJAIN - 371559
  6. SAGAR - 272974
  7. CHHINDWARA - 268971
  8. RATLAM - 258581
  9. REWA - 242377
  10. DEWAS - 240930


Link: https://plot.ly/~shashank-sharma/19/

Which color does people prefer while buying any transport vehicle ?


  1. BLACK - 2137200
  2. RED - 683663
  3. NOT SPECIFY - 560975
  4. BLUE - 341134
  5. GREY - 288952
  6. WHITE - 283631
  7. SILVER - 255836
  8. RBK - 238896
  9. P BLACK - 177379
  10. PBK - 168518



Link: https://plot.ly/~shashank-sharma/11/

Which company have its maximum vehicle in MP ?


  1. HERO HONDA MOTORS - 2032369
  2. BAJAJ AUTO LTD - 1677867
  3. HERO MOTO CORP LTD. - 1563023
  4. TVS MOTOR CO. LTD. - 1130974
  5. HONDA MCY & SCOOTER P I LTD - 1102624
  6. MAHINDRA & MAHINDRA LTD - 463175
  7. TATA MOTORS LTD - 280684
  8. MARUTI SUZUKI INDIA LIMITED - 258392
  9. MARUTI UDYOG LTD - 249949
  10. ESCORTS LTD - 139231



Link: https://plot.ly/~shashank-sharma/13/


In which year does maximum vehicle were issued ?


  1. 2016 - 1406802
  2. 2014 - 1392520
  3. 2015 - 1166079
  4. 2013 - 964026
  5. 2011 - 845374
  6. 2012 - 734092
  7. 2010 - 716772
  8. 2009 - 607693
  9. 2008 - 481315
  10. 2007 - 471963



Link: https://plot.ly/~shashank-sharma/15/
Which transport vehicle is in majority ?

  1. SPLENDOR PLUS - 325878
  2. PLATINA - 302537
  3. HF DELUXE SELF CAST WHEEL - 254166
  4. ACTIVA (ELE AUTO & KICK START) - 216252
  5. TVS STAR CITY - 210397
  6. CD DLX - 188885
  7. DISCOVER DTS - SI - 180193
  8. PASSION PRO(DRM-SLF CASTWHEEL) - 163088
  9. ACTIVA 3G EAS KS CBS BS3 - 162542
  10. PASSION PLUS - 146584



Link: https://plot.ly/~shashank-sharma/17/
What type of vehicle does people have in majority ?


  1. MOTOR CYCLE - 6531708
  2. SCOOTER - 1291932
  3. MOTOR CAR - 881930
  4. TRACTOR - 687360
  5. GOODS TRUCK - 210932
  6. MOPED - 197450
  7. OMNI BUS FOR PRIVATE USE - 142478
  8. AUTO RICKSHAW PASSENGER - 124051
  9. TROLLY - 111358
  10. PICK UP VAN - 95238


Link: https://plot.ly/~shashank-sharma/9/

And that's how many more questions can be solved with the given data.
Thank you for reading till the end of this page. I hope by now you realized the real power of python.


Source code: https://github.com/shashank-sharma/MP-Transportation-Analysis

Loved it ? Check out my github blog here : http://shashank-sharma.github.io/