Ilham Learning
Ilham Learning

Reputation: 57

BeautifulSoup4 Print output find_all() as array one by one

I'm trying to scrape data from the URL and print out them 1 by 1. Below is my code :

import requests
from bs4 import BeautifulSoup
from pandas import DataFrame

header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/87.0.4280.88 Safari/537.36 '
}

def scraping(url):

    response = requests.get(str(url), headers=header)
    soup = BeautifulSoup(response.text, 'html.parser')

    table = soup.find('table', {'class': 'stock_details table table-hover table-striped table-theme'})

    for listnumber in table.find_all('td', {'class':'number'}):
        print(listnumber.get_text())
        
        length = 11 
        #as output I got has 11 line. But actually I prefer something like 'length = len(listnumber)'
        
        for i in range(len(listnumber)):
            print(listnumber[3].get_text()) 
            #expecting value '10,200 / 2,600' would appear

scrapping('https://www.klsescreener.com/v2/stocks/view/5292')

Right now, I only able to print only all the data but still couldn't print them individually. Not only print, but I also plan to use the selected data for some equation later.'

That's why I'm thinking converting those output into array, but it still failed.

Can somebody please help? Thanks a lot!

Upvotes: 2

Views: 298

Answers (1)

ABC
ABC

Reputation: 645

I assumed that you wish to extract all the numbers from the table, then the line print(listnumber.get_text()) isn't what you are looking for?

i.e. you could store the result into array:

def scraping(url):
    result = [listnumber.get_text() for listnumber in table.find_all('td', {'class':'number'})]
    ...

Outputs:

['10.980', '10.480','1,285,600', '10,200 / 2,600', '10.740 / 10.760', '1.000 - 10.980', '28.86', '87', '12.41', '2.00', '0.19%', '0.4300', '25.02', '44.28', '24', '5,920.2M', '550.20']

Upvotes: 1

Related Questions