Home > Mobile >  Scarpy not able to scrape simple website as it gives empty data
Scarpy not able to scrape simple website as it gives empty data

Time:06-05

I am working on following website but I am getting strange error as all table data are empty.

Below is my simple scrapy code.

class FloorSheetSpider(scrapy.Spider):
    name = "nepse"
    start_urls = ['https://merolagani.com/CompanyDetail.aspx?symbol=AKPL']

    def parse(self, response):
        datas = response.xpath("//tbody[@class='panel panel-default']//tr")
        for quote in datas:
            d = quote.css('td a::text').extract()
            print(d)

what I am getting in console is empty array I know I missed something here but cannot figure it out sadly.

CodePudding user response:

To get all table data, you can follow the next example:

import scrapy
from scrapy.crawler import CrawlerProcess
class MeroSpider(scrapy.Spider):
    name = 'mero'
    allowed_domains = ['nl.indeed.com']
    start_urls = ['https://merolagani.com/CompanyDetail.aspx?symbol=AKPL']

    def parse(self, response):
        for tr in response.xpath('//*[@id="accordion"]/tbody/tr')[1:]:
            yield {
                'sector':''.join([x.replace('#\r\n','').replace('\r\n','').strip() for x in tr.xpath('.//th//text()').getall()]),
                'hydro power': ''.join([ x .replace('#\r\n','').replace('\r\n','').strip() for x in tr.xpath('.//td//text()').getall()])
            }

             
if __name__ == "__main__":
    process = CrawlerProcess()
    process.crawl(meroSpider)
    process.start()

Output:

{'sector': 'Shares Outstanding', 'hydro power': '18,552,105.00'}
2022-06-05 00:13:42 [scrapy.core.scraper] DEBUG: Scraped from <200 https://merolagani.com/CompanyDetail.aspx?symbol=AKPL>
{'sector': 'Market Price', 'hydro power': '448.00'}
2022-06-05 00:13:42 [scrapy.core.scraper] DEBUG: Scraped from <200 https://merolagani.com/CompanyDetail.aspx?symbol=AKPL>
{'sector': '% Change', 'hydro power': '-1.32 %'}
2022-06-05 00:13:42 [scrapy.core.scraper] DEBUG: Scraped from <200 https://merolagani.com/CompanyDetail.aspx?symbol=AKPL>
{'sector': 'Last Traded On', 'hydro power': '2022/06/03 03:00:00'}
2022-06-05 00:13:42 [scrapy.core.scraper] DEBUG: Scraped from <200 https://merolagani.com/CompanyDetail.aspx?symbol=AKPL>
{'sector': '52 Weeks High - Low', 'hydro power': '882.40-377.30'}
2022-06-05 00:13:42 [scrapy.core.scraper] DEBUG: Scraped from <200 https://merolagani.com/CompanyDetail.aspx?symbol=AKPL>
{'sector': '180 Day Average', 'hydro power': '498.83'}
2022-06-05 00:13:42 [scrapy.core.scraper] DEBUG: Scraped from <200 https://merolagani.com/CompanyDetail.aspx?symbol=AKPL>
{'sector': '120 Day Average', 'hydro power': '479.55'}
2022-06-05 00:13:42 [scrapy.core.scraper] DEBUG: Scraped from <200 https://merolagani.com/CompanyDetail.aspx?symbol=AKPL>
{'sector': '1 Year Yield', 'hydro power': '2.05%'}
2022-06-05 00:13:42 [scrapy.core.scraper] DEBUG: Scraped from <200 https://merolagani.com/CompanyDetail.aspx?symbol=AKPL>
{'sector': 'EPS', 'hydro power': '16.12(FY:078-079, Q:3)'}
2022-06-05 00:13:42 [scrapy.core.scraper] DEBUG: Scraped from <200 https://merolagani.com/CompanyDetail.aspx?symbol=AKPL>
{'sector': 'P/E Ratio', 'hydro power': '27.79'}
2022-06-05 00:13:42 [scrapy.core.scraper] DEBUG: Scraped from <200 https://merolagani.com/CompanyDetail.aspx?symbol=AKPL>
{'sector': 'Book Value', 'hydro power': '112.55'}
2022-06-05 00:13:42 [scrapy.core.scraper] DEBUG: Scraped from <200 https://merolagani.com/CompanyDetail.aspx?symbol=AKPL>
{'sector': 'PBV', 'hydro power': '3.98'}
2022-06-05 00:13:42 [scrapy.core.scraper] DEBUG: Scraped from <200 https://merolagani.com/CompanyDetail.aspx?symbol=AKPL>
{'sector': '% Dividend', 'hydro power': '0.89(FY:077-078)'}
2022-06-05 00:13:42 [scrapy.core.scraper] DEBUG: Scraped from <200 https://merolagani.com/CompanyDetail.aspx?symbol=AKPL>
{'sector': '#Fiscal YearValue', 'hydro power': '#Fiscal YearValue1.0.89%(FY: 077-078)2.0.29%(FY: 076-077)'}
2022-06-05 00:13:42 [scrapy.core.scraper] DEBUG: Scraped from <200 https://merolagani.com/CompanyDetail.aspx?symbol=AKPL>
{'sector': '% Bonus', 'hydro power': '17.00(FY:077-078)'}
2022-06-05 00:13:42 [scrapy.core.scraper] DEBUG: Scraped from <200 https://merolagani.com/CompanyDetail.aspx?symbol=AKPL>
{'sector': '#ValueFiscal Year', 'hydro power': '#ValueFiscal Year1.17.00%(FY: 077-078)2.5.71%(FY: 076-077)'}
2022-06-05 00:13:42 [scrapy.core.scraper] DEBUG: Scraped from <200 https://merolagani.com/CompanyDetail.aspx?symbol=AKPL>
{'sector': 'Right Share', 'hydro power': ''}
2022-06-05 00:13:42 [scrapy.core.scraper] DEBUG: Scraped from <200 https://merolagani.com/CompanyDetail.aspx?symbol=AKPL>
{'sector': '#ValueFiscal Year', 'hydro power': '#ValueFiscal Year'}
2022-06-05 00:13:42 [scrapy.core.scraper] DEBUG: Scraped from <200 https://merolagani.com/CompanyDetail.aspx?symbol=AKPL>
{'sector': '30-Day Avg Volume', 'hydro power': '70,002.00'}
2022-06-05 00:13:42 [scrapy.core.scraper] DEBUG: Scraped from <200 https://merolagani.com/CompanyDetail.aspx?symbol=AKPL>
{'sector': 'Market Capitalization', 'hydro power': '8,311,343,040.00'}
2022-06-05 00:13:42 [scrapy.core.engine] INFO: Closing spider (finished)
2022-06-05 00:13:42 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 244,
 'downloader/request_count': 1,
 'downloader/request_method_count/GET': 1,
 'downloader/response_bytes': 130664,
 'downloader/response_count': 1,
 'downloader/response_status_count/200': 1,
 'elapsed_time_seconds': 1.315064,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2022, 6, 4, 18, 13, 42, 407891),
 'item_scraped_count': 20,

CodePudding user response:

remove a from your selector, it should be -

quote.css('td ::text').extract()

Then you will get the data

  • Related