Skip to main content

Web Scraping using Beautiful Soup (NEPSE LIVE DATA SCRAPING IN PYTHON)

from bs4 import BeautifulSoup
import requests
import csv


source = requests.get("http://nepalstock.com.np/").text
soup = BeautifulSoup(source, 'lxml')

csv_file = open('nepse.csv','w')

csv_writer = csv.writer(csv_file)
csv_writer.writerow(['Symbol''Values''Total Traded amount'])

marquee_tag = soup.find(
    'div'class_="col-xs-10 col-md-10 col-sm-12").marquee.b

for span_tag in marquee_tag('span'):
    span_tag.replace_with('')

for img_tag in marquee_tag('img'):
    img_tag.replace_with('')

symbol_list = marquee_tag.text.split('( )')

all_symbols = []
short_name = []
per_share_value = []
total_traded_amount = []
a1 = []
a2 = []
a3 = []

for symbol in symbol_list:
    symbol_name = symbol.replace(u'\xa0'u'')
    all_symbols.append(symbol_name)
all_symbols.pop()

a = len(all_symbols)

for i in range(a):
    a1.append(all_symbols[i].strip())
    a2.append(a1[i].split(' '))

for i in range(len(a1)):
    short_name.append(a2[i][0])
    per_share_value.append(a2[i][1])
    total_traded_amount.append(a2[i][3])
    csv_writer.writerow([a2[i][0],a2[i][1],a2[i][3]])
# print(total_traded_amount)
csv_file.close()

Comments

  1. Need some help. How do I append company values ( for ex. 360, 2845) to the url and save each page with the key string ( like ADBL.html, AHPC.html) .

    #dict
    comp_dict = {'ADBL':'397', 'AHPC':'360', 'AKBSL':'2845', 'AKJCL':'2788', 'AKPL':'2757'}
    comp = comp_dict.values()
    compids = str(comp)

    url = "http://www.nepalstock.com.np/company/display/"

    with urllib.request.urlopen(url + compids) as url:

    out_file = open(compids[key] +".html", "w")

    out_file.close()


    ReplyDelete
  2. Isn't scrapping Nepse's Website illegal.

    ReplyDelete

Post a Comment

Popular posts from this blog

How to write PROLOG program for half adder?

In this problem, we are going to implement some logic (of course it is all about logic) that will run calculate the half adder sum and carry. We know has two inputs, and it will give sum and carry which looks like this: X            Y                 Sum                 Carry 0            0                  0                      0 0            1                  1                      0          1            0                  1                      0 1            1                  0                      1 So, to implement sum we need XOR gate and to implement Carry we need AND gate. Here is how we do it: xORGate(0,0,0). xORGate(0,0,0). xORGate(0,0,0). xORGate(0,0,0). aNDGate(0,0,0). aNDGate(0,0,0). aNDGate(0,0,0). aNDGate(0,0,0). halfadder(X,Y,S,C):-xORGate(X,Y,S), aNDGate(X,Y,C).  Now, you can run this code by giving the values of X and Y. like this: halfadder(1,1,S,C)   and the output will be S=0 C=1

How to generate Golden Ratio by programming?

Golden ratio has been a fascinating topic for last more than 2400 years despite it does not have reliable evidence. Golden ratio is seen in the circle of Sunflower "flower". We can get golden ratio when we divide a line into two parts, the ratio of dividing the large part with small part is equal to the whole length divided by the longer part. For a programmer it will be more fascinating to know how to code the golden ratio. The most easy way to do this is by using Fibonacci Series. We can use python code like this. s = 0 a = 1 b = 1 for x in range(9999): c = a + b a = b print (c/b)  #This is the golden ratio b = c Golden ratio occurs in nature in different forms. Some of the examples are: 1. Flower Petals 2. Seed Heads 3. Pine Cones 4. Shells 5. Spiral galaxies 6. Hurricanes 7. Fingers 8. Animal Bodies 9. DNA molecules Thank you! A blog by Saroj Bhattarai