Web Scraping - 如何提取股票价格 [英] Web Scraping - how to extract stock price

查看:53
本文介绍了Web Scraping - 如何提取股票价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个使用符号提取股票价格的网络抓取 python 代码(使用 2.7.11).我不确定为什么这不起作用.但它给了我这个输出:

I am creating a web scraping python code (using 2.7.11) that extracts the stock price using symbol. I am not sure why this is not working. But it gives me this output:

Enter Financial Symbol

appl YPE h
Do you want to run again?

我的代码如下:

import urllib

go=True

while go:
    print "Enter Financial Symbol"
    symbol=raw_input()

    page=urllib.urlopen("http://finance.yahoo.com/q?s=" + symbol)

    text=page.read()
    where=text.find("yfs_l84")
  
    start=where+7
    end=start+5

    result = text[start:end]
    print ( symbol + " "+ result)


    print "Do you want to run again?"
    choice=raw_input()
    if choice == "no":
        go=False

我该如何让它发挥作用?

How do I make it work?

推荐答案

您不应该使用 str.find 来解析网页,而应该使用 bs4 但在这种情况下,有一个 api,您可以请求 json 格式的数据,结合 requests 使得获取数据变得非常简单.

You should not be using str.find to parse a webpage, you should use an html parser like bs4 but in this case there is an api that you can requests the data from in json format, combining with requests makes it pretty trivial to get the data.

In [25]: import  requests

In [26]: sym = "DVN"    
In [27]: r = requests.get("http://finance.yahoo.com/webservice/v1/symbols/{sym}/quote?format=json".format(sym=sym)) 
In [28]: r.json()
Out[28]: 
{'list': {'meta': {'count': 1, 'start': 0, 'type': 'resource-list'},
  'resources': [{'resource': {'classname': 'Quote',
     'fields': {'name': 'Devon Energy Corporation Common',
      'price': '18.650000',
      'symbol': 'DVN',
      'ts': '1455915714',
      'type': 'equity',
      'utctime': '2016-02-19T21:01:54+0000',
      'volume': '33916489'}}}]}}

In [29]: sym = "YHOO"
In [30]: r = requests.get("http://finance.yahoo.com/webservice/v1/symbols/{sym}/quote?format=json".format(sym=sym))
In [31]: r.json()
Out[31]: 
{'list': {'meta': {'count': 1, 'start': 0, 'type': 'resource-list'},
  'resources': [{'resource': {'classname': 'Quote',
     'fields': {'name': 'Yahoo! Inc.',
      'price': '30.040001',
      'symbol': 'YHOO',
      'ts': '1455915600',
      'type': 'equity',
      'utctime': '2016-02-19T21:00:00+0000',
      'volume': '20734985'}}}]}}

In [32]: sym = "AAPL"
In [33]: r = requests.get("http://finance.yahoo.com/webservice/v1/symbols/{sym}/quote?format=json".format(sym=sym))
In [34]: r.json()
Out[34]: 
{'list': {'meta': {'count': 1, 'start': 0, 'type': 'resource-list'},
  'resources': [{'resource': {'classname': 'Quote',
     'fields': {'name': 'Apple Inc.',
      'price': '96.040001',
      'symbol': 'AAPL',
      'ts': '1455915600',
      'type': 'equity',
      'utctime': '2016-02-19T21:00:00+0000',
      'volume': '35374173'}}}]}}

您可以使用按键访问来提取您想要的任何数据,这比 str.find 强大得多.

You can pull whatever data you want using accessing by key which is a lot more robust than str.find.

这篇关于Web Scraping - 如何提取股票价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆