python - 如何使用 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)
解决方案
您可以循环浏览所有表格,然后选择具有所需信号类型的行。例如:
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']
推荐阅读
- flutter - 滑动轮播滑块 Flutter 时,LinearPogressIndicator 增加其值
- java - 使用ScrollView时Android java状态栏颜色不透明
- azure - 在 Azure IoT Hub 中,如何查找每个设备 ID 使用的每日配额使用情况(设备到云遥测)?
- python - 在没有安装 pip 的包的情况下在 Conda 环境中运行
- python - 腌制对象并将其加载到 numpy 数组中
- python - py_call_impl 中的错误,当在 R 的 phate 中使用 knn.dist.method = 'precomputed'
- python - 如何使用 Django 对两个以上的表进行连接?
- javascript - req.files keeps returning undefined
- regex - 正则表达式:将接下来的两行(选项卡)放在第一个返回
- python - 完整的 Python 类