Scrapy 无法抓取简单的网站,因为它提供空数据
Scarpy not able to scrape simple website as it gives empty data
我正在努力关注 website 但我收到奇怪的错误,因为所有 table 数据都是空的。
下面是我简单的 scrapy 代码。
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)
我在控制台中得到的是空数组我知道我在这里遗漏了一些东西但遗憾的是无法弄清楚。
从您的选择器中删除 a
,它应该是 -
quote.css('td ::text').extract()
然后你会得到数据
要获取所有table数据,您可以按照下一个例子:
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,
我正在努力关注 website 但我收到奇怪的错误,因为所有 table 数据都是空的。
下面是我简单的 scrapy 代码。
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)
我在控制台中得到的是空数组我知道我在这里遗漏了一些东西但遗憾的是无法弄清楚。
从您的选择器中删除 a
,它应该是 -
quote.css('td ::text').extract()
然后你会得到数据
要获取所有table数据,您可以按照下一个例子:
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,