# Automatically get iShares ETF holdings

I heard that ETF's must publicly report their holdings all the time. I have seen that for example on the iShares website I can download the list of holdings as a csv file:

https://www.ishares.com/us/products/239705/ishares-phlx-semiconductor-etf

I imagine that there is a way to access these holdings for free automatically, maybe with some API? I checked the Blackrock API but on the main page, I didn't see any info for ETF's on the 'portfolio analysis' and 'search securities' tabs.

I'm new to interacting with the web, so maybe my best bet would just be to google how to pull downloadables from webpages?

Any thoughts?

No need to scrape the site. That should always be a last resort. The below will import the .csv file you are asking about and save it to a directory of your choice. If you don't want to specify a directory can eliminate dir and any references to it and the file will go straight to your working directory. I usually save data separately hence that option.

from urllib.request import urlretrieve
import pandas as pd

dir = '[Your directory of choice]'

url = 'https://www.ishares.com/us/products/239705/ishares-phlx-semiconductor-etf/\
1467271812596.ajax?fileType=csv&fileName=SOXX_holdings&dataType=fund'

urlretrieve(url, dir + 'SOXX_holdings.csv')

df = pd.read_csv(dir + 'SOXX_holdings.csv', skiprows=10)


Alternate to above: importing data directly into a pandas dataframe instead of saving it locally by passing url as an argument.

import pandas as pd

url = 'https://www.ishares.com/us/products/239705/ishares-phlx-semiconductor-etf/\
1467271812596.ajax?fileType=csv&fileName=SOXX_holdings&dataType=fund'

df = pd.read_csv(url, skiprows=10)

Furthermore, using pd.read_xls does not work as the file seems to be xml related. (apparently also libreoffice cannot deal with the file)