Py学习  »  Python

有限的选项链信息python

Tom • 3 年前 • 1287 次点击  

我试图检索800多个股票代码(例如AAPL、IBM、JPM)的一些期权链信息(期权链的第一个到期日),但是当我运行下面的代码时,它似乎只检索到119个值,我试图捕捉错误,但我仍然没有得到所有的值。这可能是什么原因?

import yfinance as yf
import pandas as pd
from datetime import date
from yahoo_fin import stock_info as si
import statistics
import requests,time
from bs4 import BeautifulSoup
import pandas_datareader.data as web


url = "https://finviz.com/screener.ashx?v=111&f=cap_large"
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36','accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'}
tickers = []

while True:
    r = requests.get(url, headers=headers)
    html = BeautifulSoup(r.text, "html.parser")

    for a in html.select('table[bgcolor="#d3d3d3"] a.screener-link-primary'):
        tickers.append(a.text)

    if html.select_one('.tab-link:-soup-contains("next")'):
        url = "https://finviz.com/"+html.select_one('.tab-link:-soup-contains("next")')['href']
    else:
        break
    time.sleep(1)

opts = []

try:
    for i in tickers:
        opts.append(yf.Ticker(i).options[0])
    
except:
    opts.append("Error")
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/130765
 
1287 次点击  
文章 [ 1 ]  |  最新文章 3 年前