6
$\begingroup$

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?

$\endgroup$
1

2 Answers 2

8
$\begingroup$

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)
print(df.head())

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)    
print(df.head())

Skipping the first 10 rows and printing the head is just how I wanted to view the data. Lot's of other things you can do from here.

Good luck.

$\endgroup$
1
  • $\begingroup$ @user8834780 click the link in the OP. When you get to the iShares page will find a link to click to download the fund info in a csv. Right click the link and inspect the element. You will find the url for the csv download in there. The expense ratio would be a different story. $\endgroup$
    – amdopt
    Commented Feb 13, 2019 at 18:59
3
$\begingroup$

I think the answer to this question is partly deprecated. The downloads have changed to .xls files. However, changing the .ajax and fileType does seem to work. However, downloading files from different countries then the us does not seem to work as probably a different .ajax file is used.

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

(btw: I don't have enough reputation points to make a comment)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.