【问题标题】:Web Scraping - how to extract stock priceWeb Scraping - 如何提取股票价格
【发布时间】:2016-06-03 03:13:15
【问题描述】:

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

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

如何让它发挥作用?

【问题讨论】:

    标签: python python-2.7 web-scraping


    【解决方案1】:

    您正在搜索的字符串"yfs_l84" 不包含在雅虎返回的HTML 中。所以

    where=text.find("yfs_l84")
    

    where 保留为-1。因此,您的切片 text[start:end] 将始终为 text[6:11] 并将 YPR h 从页面源中删除:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en-US">
    <head>
        ...
    

    【讨论】:

      【解决方案2】:

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

      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 更强大。

      【讨论】:

        猜你喜欢
        • 2015-06-20
        • 2020-08-09
        • 2011-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多