首页 > 解决方案 > 如何使用 Python 和 BeautifulSoup 或 Pandas 从 Finviz.com 刮取 Top Gainers 和 Top Losers 的表格行?

问题描述

这是我所得到的。如何使用 Python 和 BeautifulSoup 或 Pandas 从 Finviz.com 刮取 Top Gainers 和 Top Losers 的表格行?

import requests
from bs4 import BeautifulSoup


r=requests.get("https://finviz.com")
c=r.content
soup = BeautifulSoup(c, "html.parser")

table =soup.find("table", {"class": "t-home-table"})
table_rows = table.find_all("tr")


for tr in table_rows:
 td = tr.find_all("td")
 row = [i.text for i in td]
 print(row)

标签: pythonweb-scrapingbeautifulsoupstock

解决方案


您可以循环浏览所有表格,然后选择具有所需信号类型的行。例如:

import requests
from bs4 import BeautifulSoup


r = requests.get("https://finviz.com")
soup = BeautifulSoup(r.content, "html.parser")

for table in soup.find_all("table", {"class": "t-home-table"}):
    print()

    for tr in table.find_all("tr"):
        td = tr.find_all("td")
        row = [i.text for i in td]

        if len(row) > 5 and row[5] in ['Top Gainers', 'Top Losers']:
            print(row)

这将显示:

['MBRX', '1.27', '0.00%', '0', '', 'Top Gainers']
['EFC', '9.12', '0.00%', '0', '', 'Top Gainers']
['NYMTP', '14.90', '0.00%', '0', '', 'Top Gainers']
['I', '1.65', '0.00%', '0', '', 'Top Gainers']
['NYMTO', '14.80', '0.00%', '0', '', 'Top Gainers']
['NYMTM', '14.43', '0.00%', '0', '', 'Top Gainers']
['XAN', '2.99', '0.00%', '0', '', 'Top Gainers']

['YGYI', '1.90', '0.00%', '0', '', 'Top Losers']
['DPW', '1.24', '0.00%', '0', '', 'Top Losers']
['EDRY', '4.52', '0.00%', '0', '', 'Top Losers']
['INVE', '2.76', '0.00%', '0', '', 'Top Losers']
['EHTH', '103.20', '0.00%', '0', '', 'Top Losers']
['SPTN', '12.66', '0.00%', '0', '', 'Top Losers']

推荐阅读