-
-
Save RGGH/d20524fd14e2e5d3280f98c128676dfe to your computer and use it in GitHub Desktop.
How to scrape tables using Scrapy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scrapy | |
import pandas | |
from ..items import YahooItem | |
class YahooSpider(scrapy.Spider): | |
name = 'Yahoo' | |
symbols = ["ADSK","BA","CAT","EBAY","GS","HSY","IBM","JPM","WMT","SHOP", | |
"T", "F", "TRI", "AMZN", "C", "A", "O", "B","MSFT", "NVDA", | |
"DIS", "AAL", "NFLX", "JNJ","BAC","GOOGL", "WFC"] | |
start_urls = ['https://finance.yahoo.com/quote/{0}/history?p={0}'.format(x) for x in symbols] | |
def parse(self, response): | |
items = YahooItem() | |
data = response.xpath('//table//text()').extract() | |
title = response.xpath('//title//text()').extract() | |
num_cols = 7 | |
output = [data[i:i + num_cols] for i in range(0, len(data), num_cols)] | |
dictionary = pandas.DataFrame(output[1:], columns=output[0]).set_index('Date').to_dict() | |
items['title'] = title | |
items['data'] = dictionary | |
yield items |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment