scrapy Scarpy无法抓取简单的网站,因为它提供空数据

krugob8w  于 2022-11-09  发布在  其他
关注(0)|答案(2)|浏览(142)

我正在处理以下website,但我得到奇怪的错误,因为所有表数据都是空的。
下面是我的简单代码。

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)
        for tr in response.xpath("//div[@id='divAbout']//table//tbody/tr"):
              print(tr)

我希望第二个表位于第一个表下面,但得到响应为空
我在控制台中得到的是空数组,我知道我在这里错过了一些东西,但遗憾的是无法弄清楚。

mgdq6dx1

mgdq6dx11#

要获取所有表数据,您可以遵循以下示例:

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()

输出:

{'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,
wrrgggsh

wrrgggsh2#

从选择器中删除a,它应该是-
quote.css('td ::text').extract()
然后您将获得数据

相关问题