Skip to content

Instantly share code, notes, and snippets.

@pebreo
Created January 26, 2014 03:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pebreo/8628100 to your computer and use it in GitHub Desktop.
Save pebreo/8628100 to your computer and use it in GitHub Desktop.
temporary file for requests+xlswriter
"""
Requirements:
XlsxWriter==0.5.2
beautifulsoup4==4.3.2
requests==2.2.1
"""
from xlsxwriter.workbook import Workbook
from bs4 import BeautifulSoup
import requests
# works
def f1():
"""Write data to excel worksheet
"""
workbook = Workbook('foo.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write(1,1,'hello world')
workbook.close()
def f2():
ticker = 'YHOO'
base_url = 'http://finance.yahoo.com/q/op?s={}+Options'.format(ticker)
print base_url
r = requests.get(base_url)
#print r.content
soup = BeautifulSoup(r.content)
table = soup.find('table',class_='yfnc_datamodoutline1')
headers = table.findAll('th',class_='yfnc_tablehead1')
for h in headers:
print h.get_text()
rows = table.findAll('tr')
print rows[0]
def f3():
ticker = 'YHOO'
base_url = 'http://finance.yahoo.com/q/op?s={}+Options'.format(ticker)
print base_url
r = requests.get(base_url)
#print r.content
soup = BeautifulSoup(r.content)
# Find table
table = soup.find('table',class_='yfnc_datamodoutline1')
# Get headers
headers = table.findAll('th',class_='yfnc_tablehead1')
# We have to find the embedded table which contains the actul data
# also, we slice the first element which is the header
rows = table.find('table',border=0).findAll('tr')[1:]
print rows
def f4():
ticker = 'YHOO'
base_url = 'http://finance.yahoo.com/q/op?s={}+Options'.format(ticker)
print base_url
r = requests.get(base_url)
#print r.content
soup = BeautifulSoup(r.content)
# Find table
table = soup.find('table',class_='yfnc_datamodoutline1')
# Get headers
headers = table.findAll('th',class_='yfnc_tablehead1')
# We have to find the embedded table which contains the actul data
# also, we slice the first element which is the header
rows = table.find('table',border=0).findAll('tr')[1:]
# write to excel one row
# http://stackoverflow.com/questions/17684610/python-convert-csv-to-xlsx
workbook = Workbook('foo2.xlsx')
worksheet = workbook.add_worksheet()
for col,h in enumerate(headers):
worksheet.write(1,col,h.get_text())
workbook.close()
#f1()
#f2() # works
#f3() # works
f4()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment