Search code examples
pythonweb-scrapingbeautifulsouptags

BeautifulSoup4 extract and select data from pre style


I want to extract all the short_name from this link I've tried already following this answer but it's failed. The result I got is 'None'.

Here is my code :

def checkStockIdExistOrNot(stockIdNumberOrName):

    BursaStockSearchIdURL = 'https://www.bursamalaysia.com/api/v1/search/stock_list?keyword=' + str(stockIdNumberOrName) + '&lang=EN&limit=99'
    BursaStockSearchIdRequest = requests.get(str(BursaStockSearchIdURL), headers=header)
    BursaStockSearchIdParser = BeautifulSoup(BursaStockSearchIdRequest.content, 'html.parser')
    BursaSelection = BursaStockSearchIdParser.find('pre')
    print(BursaSelection)

checkStockIdExistOrNot('SERBADK')

My intention is to get only the short_name SERBADK and SERBADK-C17. But, coz of the 'None' value, I can't select/pick any single data out of it.

Thanks!


Solution

  • As request is returning data as json format so you can use direct .json method to extract data from it!

    import requests
    res=requests.get("https://www.bursamalaysia.com/api/v1/search/stock_list?keyword=SERBADK&lang=EN&limit=99")
    main_data=res.json()['data']
    for i in range(len(main_data)):
        print(main_data[i]['short_name'])
    

    Output:

    SERBADK
    SERBADK-C16
    SERBADK-C17
    SERBADK-C20
    SERBADK-C21
    SERBADK-C22
    SERBADK-C23
    SERBADK-C24
    SERBADK-C25
    SERBADK-C26
    SERBADK-WA
    

    For finding first element you can use

    main_data[0]['short_name']

    as main_data return as list you can iterate using index value