我一直试图用我编写的这段代码从新的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)
谢谢你们。
答案 0 :(得分:1)
您必须拨打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:\u002F\u002Fstocks.us.reuters.com\u002Fstocks\u002Fratios.asp?rpc=66\u0026symbol=AAPL.O",
"f_recent_quarter_date": "Q4 (Dec \u002717)",
"f_annual_date": "2017",
"f_ttm_date": "2016",
"financials": [{
"f_type": "Income\u0026nbsp\u003BStatement",
"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": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "4.39",
"annual": "9.21",
"ttm": "8.31"
}
]
},
{
"f_type": "Balance\u0026nbsp\u003BSheet",
"url": "http://www.google.com/finance?fstype=bi&q=NASDAQ:AAPL",
"f_figures": [{
"title": "",
"recent_quarter": "17,962.00",
"annual": "12,307.00",
"ttm": "11,883.00"
},
{
"title": "",
"recent_quarter": "49,662.00",
"annual": "53,892.00",
"ttm": "46,671.00"
},
{
"title": "",
"recent_quarter": "77,153.00",
"annual": "74,181.00",
"ttm": "67,155.00"
},
{
"title": "",
"recent_quarter": "23,440.00",
"annual": "17,874.00",
"ttm": "15,754.00"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "50,899.00",
"annual": "35,673.00",
"ttm": "29,299.00"
},
{
"title": "",
"recent_quarter": "4,421.00",
"annual": "4,855.00",
"ttm": "2,132.00"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "11,337.00",
"annual": "13,936.00",
"ttm": "8,283.00"
},
{
"title": "",
"recent_quarter": "143,810.00",
"annual": "128,645.00",
"ttm": "106,869.00"
},
{
"title": "",
"recent_quarter": "77,110.00",
"annual": "75,076.00",
"ttm": "61,245.00"
},
{
"title": "",
"recent_quarter": "-43,431.00",
"annual": "-41,293.00",
"ttm": "-34,235.00"
},
{
"title": "",
"recent_quarter": "5,889.00",
"annual": "5,717.00",
"ttm": "5,414.00"
},
{
"title": "",
"recent_quarter": "2,149.00",
"annual": "2,298.00",
"ttm": "3,206.00"
},
{
"title": "",
"recent_quarter": "207,944.00",
"annual": "194,714.00",
"ttm": "170,430.00"
},
{
"title": "",
"recent_quarter": "13,323.00",
"annual": "10,162.00",
"ttm": "8,757.00"
},
{
"title": "",
"recent_quarter": "406,794.00",
"annual": "375,319.00",
"ttm": "321,686.00"
},
{
"title": "",
"recent_quarter": "62,985.00",
"annual": "49,049.00",
"ttm": "37,294.00"
},
{
"title": "",
"recent_quarter": "26,281.00",
"annual": "25,744.00",
"ttm": "20,951.00"
},
{
"title": "",
"recent_quarter": "11,980.00",
"annual": "11,977.00",
"ttm": "8,105.00"
},
{
"title": "",
"recent_quarter": "6,498.00",
"annual": "6,496.00",
"ttm": "3,500.00"
},
{
"title": "",
"recent_quarter": "8,044.00",
"annual": "7,548.00",
"ttm": "9,156.00"
},
{
"title": "",
"recent_quarter": "115,788.00",
"annual": "100,814.00",
"ttm": "79,006.00"
},
{
"title": "",
"recent_quarter": "103,922.00",
"annual": "97,207.00",
"ttm": "75,427.00"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "103,922.00",
"annual": "97,207.00",
"ttm": "75,427.00"
},
{
"title": "",
"recent_quarter": "122,400.00",
"annual": "115,680.00",
"ttm": "87,032.00"
},
{
"title": "",
"recent_quarter": "548.00",
"annual": "31,504.00",
"ttm": "26,019.00"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "46,337.00",
"annual": "11,747.00",
"ttm": "12,985.00"
},
{
"title": "",
"recent_quarter": "266,595.00",
"annual": "241,272.00",
"ttm": "193,437.00"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "36,447.00",
"annual": "35,867.00",
"ttm": "31,251.00"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "104,593.00",
"annual": "98,330.00",
"ttm": "96,364.00"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "-907.00",
"annual": "-26.00",
"ttm": "596.00"
},
{
"title": "",
"recent_quarter": "140,199.00",
"annual": "134,047.00",
"ttm": "128,249.00"
},
{
"title": "",
"recent_quarter": "406,794.00",
"annual": "375,319.00",
"ttm": "321,686.00"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "5,081.65",
"annual": "5,126.20",
"ttm": "5,336.17"
}
]
},
{
"f_type": "Cash\u0026nbsp\u003BFlow",
"url": "http://www.google.com/finance?fstype=ci&q=NASDAQ:AAPL",
"f_figures": [{
"title": "",
"recent_quarter": "20,065.00",
"annual": "48,351.00",
"ttm": "45,687.00"
},
{
"title": "",
"recent_quarter": "2,745.00",
"annual": "10,157.00",
"ttm": "10,505.00"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "-33,737.00",
"annual": "5,966.00",
"ttm": "4,938.00"
},
{
"title": "",
"recent_quarter": "1,285.00",
"annual": "4,674.00",
"ttm": "4,210.00"
},
{
"title": "",
"recent_quarter": "37,935.00",
"annual": "-5,550.00",
"ttm": "484.00"
},
{
"title": "",
"recent_quarter": "28,293.00",
"annual": "63,598.00",
"ttm": "65,824.00"
},
{
"title": "",
"recent_quarter": "-2,964.00",
"annual": "-12,795.00",
"ttm": "-13,548.00"
},
{
"title": "",
"recent_quarter": "-10,626.00",
"annual": "-33,651.00",
"ttm": "-32,429.00"
},
{
"title": "",
"recent_quarter": "-13,590.00",
"annual": "-46,446.00",
"ttm": "-45,977.00"
},
{
"title": "",
"recent_quarter": "-1,038.00",
"annual": "-1,247.00",
"ttm": "-1,163.00"
},
{
"title": "",
"recent_quarter": "-3,339.00",
"annual": "-12,769.00",
"ttm": "-12,150.00"
},
{
"title": "",
"recent_quarter": "-10,095.00",
"annual": "-32,345.00",
"ttm": "-29,227.00"
},
{
"title": "",
"recent_quarter": "6,971.00",
"annual": "29,014.00",
"ttm": "22,057.00"
},
{
"title": "",
"recent_quarter": "-7,501.00",
"annual": "-17,347.00",
"ttm": "-20,483.00"
},
{
"title": "",
"recent_quarter": "-",
"annual": "-",
"ttm": "-"
},
{
"title": "",
"recent_quarter": "7,202.00",
"annual": "-195.00",
"ttm": "-636.00"
},
{
"title": "",
"recent_quarter": "623.00",
"annual": "2,092.00",
"ttm": "1,316.00"
},
{
"title": "",
"recent_quarter": "3,551.00",
"annual": "11,591.00",
"ttm": "10,444.00"
}
]
}
],
"kr_recent_quarter_date": "Q4 (Dec \u002717)",
"kr_annual_date": "2017",
"kr_ttm_date": "TTM",
"keyratios": [{
"title": "Net profit margin",
"recent_quarter": "25.67%",
"annual": "21.09%",
"ttm": "22.21%"
},
{
"title": "Operating margin",
"recent_quarter": "29.76%",
"annual": "26.76%",
"ttm": "26.87%"
},
{
"title": "EBITD margin",
"recent_quarter": "",
"annual": "30.86%",
"ttm": "31.01%"
},
{
"title": "Return on average assets",
"recent_quarter": "23.25%",
"annual": "13.87%",
"ttm": "14.40%"
},
{
"title": "Return on average equity",
"recent_quarter": "66.30%",
"annual": "36.87%",
"ttm": "38.98%"
},
{
"title": "Employees",
"recent_quarter": "123,000",
"annual": "-",
"ttm": "-"
}
],
"c": "-0.58",
"l": "178.39",
"cp": "-0.32",
"ccol": "chr",
"op": "179.10",
"hi": "180.48",
"lo": "178.16",
"vo": "38.93M",
"avvo": "44.16M",
"hi52": "180.48",
"lo52": "136.28",
"mc": "905.15B",
"pe": "17.45",
"fwpe": "",
"beta": "1.33",
"eps": "10.22",
"dy": "1.41",
"ldiv": "0.63",
"shares": "5.07B",
"instown": "63%",
"eo": "",
"sid": "us-TRBC:57",
"sname": "Technology",
"iid": "us-TRBC:5710601010",
"iname": "Computer Hardware - NEC",
"related": [{
"id": "22144",
"name": "Apple Inc.",
"t": "AAPL",
"e": "NASDAQ",
"l": "178.39",
"c": "-0.58",
"mc": "905.15B",
"cp": "-0.32",
"ccol": "chr"
},
{
"id": "358464",
"name": "Microsoft Corporation",
"t": "MSFT",
"e": "NASDAQ",
"l": "94.20",
"c": "-1.22",
"mc": "725.32B",
"cp": "-1.28",
"ccol": "chr"
},
{
"id": "694653",
"name": "Alphabet Inc",
"t": "GOOGL",
"e": "NASDAQ",
"l": "1,117.51",
"c": "-26.19",
"mc": "776.61B",
"cp": "-2.29",
"ccol": "chr"
},
{
"id": "17154",
"name": "HP Inc",
"t": "HPQ",
"e": "NYSE",
"l": "23.52",
"c": "+0.05",
"mc": "38.70B",
"cp": "0.19",
"ccol": "chg"
},
{
"id": "33095",
"name": "Sony Corp (ADR)",
"t": "SNE",
"e": "NYSE",
"l": "50.27",
"c": "-0.63",
"mc": "62.92B",
"cp": "-1.24",
"ccol": "chr"
},
{
"id": "664477",
"name": "Infineon Technologies AG (ADR)",
"t": "IFNNY",
"e": "OTCMKTS",
"l": "27.28",
"c": "-0.43",
"mc": "30.65B",
"cp": "-1.55",
"ccol": "chr"
},
{
"id": "180086490123759",
"name": "SAMSUNG ELECTRONIC",
"t": "SSNNF",
"e": "OTCMKTS",
"l": "0.00000",
"c": "",
"mc": "",
"cp": "",
"ccol": ""
},
{
"id": "4112",
"name": "Adobe Systems Incorporated",
"t": "ADBE",
"e": "NASDAQ",
"l": "208.82",
"c": "-3.33",
"mc": "102.65B",
"cp": "-1.57",
"ccol": "chr"
},
{
"id": "5494267",
"name": "NEC CORP",
"t": "NIPNF",
"e": "OTCMKTS",
"l": "30.00",
"c": "+4.20",
"mc": "672.02M",
"cp": "16.28",
"ccol": "chg"
},
{
"id": "666634",
"name": "Synaptics, Incorporated",
"t": "SYNA",
"e": "NASDAQ",
"l": "47.08",
"c": "-1.13",
"mc": "1.62B",
"cp": "-2.34",
"ccol": "chr"
},
{
"id": "655931",
"name": "Panasonic Corporation (ADR)",
"t": "PCRFY",
"e": "OTCMKTS",
"l": "15.70",
"c": "-0.27",
"mc": "37.69B",
"cp": "-1.69",
"ccol": "chr"
}
],
"summary": [{
"reuters_url": "http:\u002F\u002Fstocks.us.reuters.com\u002Fstocks\u002FfullDescription.asp?rpc=66\u0026symbol=AAPL.O",
"address": "1 Apple Park Way, CUPERTINO, CA 95014-0642, United States",
"phone": "+1-408-9961010",
"url": "https:\u002F\u002Fwww.apple.com\u002F",
"overview": "Apple Inc. (Apple) designs, manufactures and markets mobile communication and media devices, personal computers, and portable digital music players, and a variety of related software, services, peripherals, networking solutions, and third-party digital content and applications. The Company\u0027s products and services include iPhone, iPad, Mac, iPod, Apple TV, a portfolio of consumer and professional software applications, the iOS and OS X operating systems, iCloud, and a variety of accessory, service and support offerings. The Company also delivers digital content and applications through the iTunes Store, App StoreSM, iBookstoreSM, and Mac App Store. The Company distributes its products worldwide through its retail stores, online stores, and direct sales force, as well as through third-party cellular network carriers, wholesalers, retailers, and value-added resellers. In February 2012, the Company acquired app-search engine Chomp."
}],
"management": [{
"name": "Art D. Levinson Ph.D.",
"age": "67",
"title": "Independent Chairman of the Board"
},
{
"name": "Timothy D. Cook",
"age": "57",
"title": "Chief Executive Officer, Director"
},
{
"name": "Luca Maestri",
"age": "54",
"title": "Chief Financial Officer, Senior Vice President"
},
{
"name": "Jeff Williams",
"age": "54",
"title": "Chief Operating Officer, Senior Vice President"
},
{
"name": "Katherine Adams",
"age": "53",
"title": "Senior Vice President, General Counsel"
},
{
"name": "Phil Schiller",
"age": "57",
"title": "Senior Vice President - Worldwide Marketing"
},
{
"name": "Angela J. Ahrendts",
"age": "57",
"title": "Senior Vice President - Retail"
},
{
"name": "Eddy Cue",
"age": "54",
"title": "Senior Vice President - Internet Software and Services"
},
{
"name": "Craig Federighi",
"age": "48",
"title": "Senior Vice President - Software Engineering"
},
{
"name": "Daniel J. Riccio Jr.",
"age": "55",
"title": "Senior Vice President - Hardware Engineering"
}
],
"moreresources": [{
"name": "Estimates",
"url": "http:\u002F\u002Fwww.marketwatch.com\u002Ftools\u002Fquotes\u002Fsnapshot.asp?symb=AAPL\u0026pg=analyst"
},
{
"name": "SEC Filings",
"url": "http:\u002F\u002Fgoogle.brand.edgar-online.com\u002F?sym=AAPL"
},
{
"name": "Major Holders",
"url": "http:\u002F\u002Finvesting.money.msn.com\u002Finvestments\u002Finstitutional-ownership?symbol=AAPL"
},
{
"name": "Options",
"url": "http:\u002F\u002Fwww.marketwatch.com\u002Ftools\u002Fquotes\u002Foptions1.asp?symb=AAPL"
},
{
"name": "Research",
"url": "http:\u002F\u002Ffinance.yahoo.com\u002Fq\u002Frr?s=AAPL"
}
],
"events": []
}]
查找更多{{3}}
注意强>
我使用的是Python 3,因此我必须将urllib
更改为urllib.request
。你可以改回来查看这项工作。