1

This is as far as I have gotten. How can i scrape table rows for Top Gainers and Top Losers from Finviz.com with Python and BeautifulSoup or Pandas?

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)
  • Does this answer your question? [How to construct data frame from Web Scraping in Python](https://stackoverflow.com/questions/61008195/how-to-construct-data-frame-from-web-scraping-in-python) – αԋɱҽԃ αмєяιcαη Apr 09 '20 at 06:31
  • You already have the first table. To get the second one, just use soup,find_all("table", ...) and the first two items will be the tables you are looking for. – Philippe F Apr 09 '20 at 06:35

1 Answers1

0

You could just cycle through all tables and then pick out the rows with the required signal type. For example:

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)

This would display:

['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']
Martin Evans
  • 45,791
  • 17
  • 81
  • 97
  • Don't forget to select an answer as the accepted solution to your question. Click the grey tick under the up/down buttons to accept it. – Martin Evans Apr 23 '20 at 12:02