繁体   English   中英

雅虎财经Web爬虫

[英]Yahoo Finance Web Crawler

我正在编写一个 web 爬虫程序,以从雅虎财经获取一家公司的股票价格。

import re
import urllib.request

stock = input('Enter a company to check its stock.')

with urllib.request.urlopen('https://finance.yahoo.com/quote/' + stock) as urlfile:
    urltext = urlfile.read()
    stock_pattern = r'(\d+)\.(\d+)'
    for i in re.findall(stock_pattern, str(urlfile.read())):
        print(i)

我如何获得一家公司的当前股价? 我怀疑它应该与for循环有关。

如果您想要非 API 方法。 查看名为 Selenium 的库。 这将让您挑选出某个元素(即当前股票价格)。

这是一个例子:

from selenium import webdriver

browser = webdriver.Chrome(executable_path='the file path of where you saved chrome driver')

browser.get('https://finance.yahoo.com/quote/' + stock)
stockPrice = find_element_by_xpath('//*[@id="quote-header-info"]/div[3]/div/div/span[1]')
print(stockPrice)

但是,是的,我也会听从评论中人们的建议,并查看一些金融网站的 api。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM