Search code examples
pythonweb-scrapingbeautifulsoupgoogle-finance

Scraping data from google finance using BeautifulSoup in python


I'm trying to get data from google finance from this link like this:

url = "https://www.google.com/finance/historical?cid=4899364&startdate=Dec+1%2C+2016&enddate=Mar+23%2C+2017&num=200&ei=4wLUWImyJs-iuASgwIKYBg"
request = urllib.request.Request(url,None,headers)
response = urllib.request.urlopen(request).read()
soup = BeautifulSoup(response, 'html.parser')
prices = soup.find_all("tbody")
print(prices)

I'm getting an empty list. I have also tried alternates like using soup.find_all('tr') but still I can't retrieve data successfully.

edit:

headers={'Host': 'www.google.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Connection': 'keep-alive'
}

Solution

  • The problem was with html.parser. I instead used lxml and it worked. Also exchanged urllib with requests.