如何使用 Python return 多行而不是只有一行?

How can I return multple rows instead of just one using Python?

我正在尝试从 finivz 中提取数据,但我始终只能提取一行。 这是我的代码:

url = ('https://finviz.com/quote.ashx?t=' + ticker.upper())
r = Request(url, headers = header)
html = urlopen(r).read()
soup = BeautifulSoup(html, 'lxml')
rows = soup.find_all('tr')
rows = rows[13:20]     
           
for row in rows:
    row_td = row.find_all('td')    <------------ I believe the issue is with this section?
#print(row_td)    

str_cells = str(row_td)
clean = BeautifulSoup(str_cells, "lxml").get_text()
print(clean)

只打印: [股息 %,2.97%,速动比率,1.30,过去 5 年的销售额,-5.70%,毛利率,60.60%,52W 低,20.59%,ATR,0.64] - 即使我指定 rows[13:30]

我想打印出 table on the page. 中的所有行 here is a screenshot of the table

在 for 循环中,您一遍又一遍地重写变量 row_td。将变量的内容存储在列表中(在我的示例中,我使用列表 all_data 来存储所有行)。

要打印 table 中的所有行,您可以使用下一个示例:

import requests
from bs4 import BeautifulSoup

headers = {
    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0"
}

url = "https://finviz.com/quote.ashx?t=KO"
soup = BeautifulSoup(requests.get(url, headers=headers).content, "lxml")

all_data = []
for tr in soup.select(".snapshot-table2 tr"):
    tds = [td.get_text(strip=True) for td in tr.select("td")]
    all_data.append(tds)

fmt_string = "{:<15}" * 12

for row in all_data:
    print(fmt_string.format(*row))

打印:

Index          DJIA S&P500    P/E            30.35          EPS (ttm)      1.87           Insider Own    0.30%          Shs Outstand   4.31B          Perf Week      -1.03%         
Market Cap     245.44B        Forward P/E    23.26          EPS next Y     2.44           Insider Trans  -2.65%         Shs Float      4.29B          Perf Month     0.30%          
Income         8.08B          PEG            3.00           EPS next Q     0.58           Inst Own       69.00%         Short Float    0.75%          Perf Quarter   3.70%          
Sales          36.41B         P/S            6.74           EPS this Y     -13.30%        Inst Trans     0.55%          Short Ratio    2.34           Perf Half Y    11.87%         
Book/sh        5.16           P/B            10.98          EPS next Y     7.84%          ROA            8.90%          Target Price   62.06          Perf Year      19.62%         
Cash/sh        3.01           P/C            18.82          EPS next 5Y    10.12%         ROE            40.10%         52W Range      46.97 - 57.56  Perf YTD       3.28%          
Dividend       1.68           P/FCF          95.02          EPS past 5Y    1.40%          ROI            12.20%         52W High       -1.60%         Beta           0.63           
Dividend %     2.97%          Quick Ratio    1.30           Sales past 5Y  -5.70%         Gross Margin   60.60%         52W Low        20.59%         ATR            0.64           
Employees      80300          Current Ratio  1.50           Sales Q/Q      41.70%         Oper. Margin   25.70%         RSI (14)       51.63          Volatility     1.17% 0.94%    
Optionable     Yes            Debt/Eq        1.89           EPS Q/Q        47.70%         Profit Margin  22.20%         Rel Volume     0.76           Prev Close     56.86          
Shortable      Yes            LT Debt/Eq     1.79           Earnings       Jul 21 BMO     Payout         87.90%         Avg Volume     13.67M         Price          56.64          
Recom          2.20           SMA20          -0.42%         SMA50          1.65%          SMA200         6.56%          Volume         10,340,772     Change         -0.39%         

您只需使用 pandas DataFrame 即可轻松完成。 这是完整的工作输出。

代码:

import requests
import pandas as pd

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36'}

url = "https://finviz.com/quote.ashx?t=KO"

req = requests.get(url,headers=headers)

wiki_table = pd.read_html(req.text, attrs = {"class":"snapshot-table2"} )

df = wiki_table[0]

print(df)

输出:

    0            1              2       3   ...            8              9             10           11
0        Index  DJIA S&P500            P/E   30.35  ...  Shs Outstand          4.31B     Perf Week       -1.03%
1   Market Cap      245.44B    Forward P/E   23.26  ...     Shs Float          4.29B    Perf Month        0.30%
2       Income        8.08B            PEG    3.00  ...   Short Float          0.75%  Perf Quarter        3.70%
3        Sales       36.41B            P/S    6.74  ...   Short Ratio           2.34   Perf Half Y       11.87%
4      Book/sh         5.16            P/B   10.98  ...  Target Price          62.06     Perf Year       19.62%
5      Cash/sh         3.01            P/C   18.82  ...     52W Range  46.97 - 57.56      Perf YTD        3.28%
6     Dividend         1.68          P/FCF   95.02  ...      52W High         -1.60%          Beta         0.63
7   Dividend %        2.97%    Quick Ratio    1.30  ...       52W Low         20.59%           ATR         0.64
8    Employees        80300  Current Ratio    1.50  ...      RSI (14)          51.63    Volatility  1.17% 0.94%
9   Optionable          Yes        Debt/Eq    1.89  ...    Rel Volume           0.76    Prev Close        56.86 
10   Shortable          Yes     LT Debt/Eq    1.79  ...    Avg Volume         13.67M         Price        56.64 
11       Recom         2.20          SMA20  -0.42%  ...        Volume       10340772        Change       -0.39% 

[12 rows x 12 columns]