谷歌财经新网站报价抓[重复]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了谷歌财经新网站报价抓[重复]相关的知识,希望对你有一定的参考价值。
这个问题在这里已有答案:
我一直试图用我写的这段代码从新的google finance site中删除引号。
import urllib
import re
import time
import requests
def get_quote(symbol):
base_url = 'http://google.com/finance?q='+ symbol
content = urllib.urlopen(base_url).read()
m = re.search('id="ref_(.*?)">(.*?)<', content)
print m
if m:
quote = m.group(2)
print quote
else:
quote = 'no quote available for: ' + symbol
return quote
while True:
get_quote('AAPL')
time.sleep(10)
当我尝试打印内容变量时,它告诉我我的请求被阻止了。
如何通过实现标头来克服这个问题?
编辑:工作代码:
from lxml import html
import time
import requests
def get_quote(symbol):
url = 'http://google.com/finance?q='+ symbol
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
page = requests.get(url, headers=headers)
tree = html.fromstring(page.content)
price = tree.xpath('//*[@id="knowledge-finance-wholepage__entity-summary"]/div/g-card-section/div/g-card-section/div[1]/span[1]/span/span[1]/text()')
print price
while True:
get_quote('AAPL')
time.sleep(10)
多谢你们。
答案
您必须调用Google财经API而不是用户应使用的网址 -
更改您的网址 -
base_url = 'https://finance.google.com/finance?q=NASDAQ:' + symbol + '&output=json'
以下是您现在必须解析的内容 -
[{
"symbol": "AAPL",
"exchange": "NASDAQ",
"id": "22144",
"t": "AAPL",
"e": "NASDAQ",
"name": "Apple Inc.",
"f_reuters_url": "http:u002Fu002Fstocks.us.reuters.comu002Fstocksu002Fratios.asp?rpc=66u0026symbol=AAPL.O",
"f_recent_quarter_date": "Q4 (Dec u002717)",
"f_annual_date": "2017",
"f_ttm_date": "2016",
"financials": [{
"f_type": "Incomeu0026nbspu003BStatement",
"url": "http://www.google.com/finance?fstype=ii&q=NASDAQ:AAPL",
"f_figures": [{
"title": "",
"recent_quarter": "88,293.00",
"annual": "229,234.00",
"ttm": "215,639.00"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "88,293.00",
"annual": "229,234.00",
"ttm": "215,639.00"
},
{
"title": "",
"recent_quarter": "54,381.00",
"annual": "141,048.00",
"ttm": "131,376.00"
},
{
"title": "",
"recent_quarter": "33,912.00",
"annual": "88,186.00",
"ttm": "84,263.00"
},
{
"title": "",
"recent_quarter": "4,231.00",
"annual": "15,261.00",
"ttm": "14,194.00"
},
{
"title": "",
"recent_quarter": "3,407.00",
"annual": "11,581.00",
"ttm": "10,045.00"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "62,019.00",
"annual": "167,890.00",
"ttm": "155,615.00"
},
{
"title": "",
"recent_quarter": "26,274.00",
"annual": "61,344.00",
"ttm": "60,024.00"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "38.00",
"annual": "-133.00",
"ttm": "-1,195.00"
},
{
"title": "",
"recent_quarter": "27,030.00",
"annual": "64,089.00",
"ttm": "61,372.00"
},
{
"title": "",
"recent_quarter": "22,665.00",
"annual": "48,351.00",
"ttm": "45,687.00"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "22,665.00",
"annual": "48,351.00",
"ttm": "45,687.00"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "20,065.00",
"annual": "48,351.00",
"ttm": "45,687.00"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "22,665.00",
"annual": "48,351.00",
"ttm": "45,687.00"
},
{
"title": "",
"recent_quarter": "20,065.00",
"annual": "48,351.00",
"ttm": "45,687.00"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "5,157.79",
"annual": "5,251.69",
"ttm": "5,500.28"
},
{
"title": "",
"recent_quarter": "4.39",
"annual": "9.21",
"ttm": "8.31"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "0.63",
"annual": "2.40",
"ttm": "2.18"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
以上是关于谷歌财经新网站报价抓[重复]的主要内容,如果未能解决你的问题,请参考以下文章