我正在尝试从 Google Finance 的 new interface 中抓取股票价格。
我使用 xpath
导航到价格位置,但在打印时,它总是返回一对方括号 []
。
我尝试修复它,但似乎没有任何效果,并且它也对屏幕上的所有其他对象执行此操作。
from lxml import html
import requests
import time
def parse(ticker):
url = "http://google.com/finance?q=%s"%(ticker)
response = requests.get(url, verify=False)
parser = html.fromstring(response.content)
priceO = parser.xpath('//*[@id="fac-ut"]/div[1]/div[4]/div[1]/span[1]/text()')
print priceO
parse('AAPL')
输出:
[]
[Finished in 1.2s]
最佳答案
您的 XPath 似乎不正确
尝试替换
priceO = parser.xpath('//*[@id="fac-ut"]/div[1]/div[4]/div[1]/span[1]/text()')
下面一行
price0 = parser.xpath('//div[@id="price-panel"]//span')[0].text_content().strip()
输出:
172.50
关于Python 抓取谷歌金融,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48948161/