加快Yahoo Finance上的网页抓取

时间:2018-08-30 12:52:32

标签: python web-scraping yahoo-finance

我正在使用Yahoo Finance和带beautifulsoup lib的python在Yahoo Finance上测试Web抓取,但运行速度非常慢。我如何加快速度?报废网站是否合法?

quotes = []
quote = {}
symbolList = ['SPY', 'AAPL']

for symbol in symbolList:
    url = ('http://finance.yahoo.com/quote/%s?p=%s' % (symbol, symbol))
    page = requests.get(url)
    html = page.text
    soup = BeautifulSoup(html, 'html.parser')

    span_latest_price = soup.find("span", {"class": "Trsdu(0.3s) Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(b)"})
    span_quote_name = soup.find('h1', {'class': 'D(ib) Fz(16px) Lh(18px)'})

    last_price = span_latest_price.text if span_latest_price else ''
    name = span_quote_name.text if span_quote_name else ''
    quote = {'name': name, 'lastPrice': last_price}
    quotes.append(quote)

0 个答案:

没有答案