简体   繁体   中英

Pandas read_html breaking

I am trying to webscrape and when 1 of the Ticker values has no info the process breaks. I would like it to continue through the list.

I tried different ways of making this work. I check and there is not a table to read from at the Yahoo site.

import pandas as pd

# The 3rd Ticker value ABMD will break this code
ticker = ["MMM", "NUVR", "ABMD" "ABT"]

for tic in ticker:
    inc_state = pd.read_html('https://finance.yahoo.com/quote/' + tic + '/financials?p' + tic, na_values=['No Acquirer'])
    df = inc_state[0][2][0]
    df3 = pd.DataFrame(inc_state)
    # This if statement doesnt work ugh!
    if df != 'No Acquirer':
        print("-" * 20)
        print(tic)
        print(df)
    else:
        continue

What I get is First 2 items work then it breaks.

MMM

12/31/2017

NUVR 12/31/2017

Traceback (most recent call last):
  File "/Users/dfalk/Google Drive/Pycharm_Working/PythonWebScrape2/fruit.py", line 9, in <module>
    inc_state = pd.read_html('https://finance.yahoo.com/quote/' + tic + '/financials?p' + tic, na_values=['No Acquirer'])
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/io/html.py", line 1094, in read_html
    displayed_only=displayed_only)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/io/html.py", line 916, in _parse
    raise_with_traceback(retained)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/compat/__init__.py", line 420, in raise_with_traceback
    raise exc.with_traceback(traceback)
ValueError: No tables found

Process finished with exit code 1

谢谢âńōŋŷxmoůŜ除了工作尝试,谢谢

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM