Skip to content

Instantly share code, notes, and snippets.

@pratapvardhan
Created June 5, 2014 09:56
Show Gist options
  • Select an option

  • Save pratapvardhan/9b57634d57f21cf3874c to your computer and use it in GitHub Desktop.

Select an option

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
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
@alejandro-jimenezsanchez

Copy link
Copy Markdown

Nice function. I am trying to implement it in python --version 3.8 and I got to this point:

from urllib.request import Request, urlopen
from lxml.html import parse

name="IBM"
req = Request('http://www.google.com/finance?&q='+name, headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req)

tree = parse(webpage)

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?

@vadik10224

vadik10224 commented Nov 29, 2020

Copy link
Copy Markdown

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