Created
June 5, 2014 09:56
-
-
Save pratapvardhan/9b57634d57f21cf3874c to your computer and use it in GitHub Desktop.
Python Script to extract Sector and Industry for a company stock listed on Google Finance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from urllib import urlopen | |
from lxml.html import parse | |
''' | |
Returns a tuple (Sector, Indistry) | |
Usage: GFinSectorIndustry('IBM') | |
''' | |
def GFinSectorIndustry(name): | |
tree = parse(urlopen('http://www.google.com/finance?&q='+name)) | |
return tree.xpath("//a[@id='sector']")[0].text, tree.xpath("//a[@id='sector']")[0].getnext().text |
Hello.
Go to web page. On MS Window's browser (Chrome). Mous right click on desireble element in web page and then "Inspect" => browser showd HTML script and selects element that you have clicked. Right click on selected item and chose simple XPath.
I recomend to find in google tutorial of XPath
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice function. I am trying to implement it in
python --version 3.8
and I got to this point:However the following line is not working and I have not been able to figured out why:
tree.xpath("//a[@id='sector']")[0].text, tree.xpath("//a[@id='sector']")[0].getnext().text
Do you have any ideas or suggestions?