user
Write a web-scraping script to extract stock information.
Response:
assistant
import requests 
import lxml.html as lh 
import pandas as pd

url = 'https://finance.yahoo.com/quote/AAPL/'
page = requests.get(url)
doc = lh.fromstring(page.content)

column_headers = []
for col in doc.xpath('//table[@class="W(100%) M(0) Bdcl(c)"]//tr[1]//th'):
    column_headers.append(col.text_content().strip())

table_rows = doc.xpath('//tr[@class = "BdT Bdc($seperatorColor) Ta(start) Fz(s) Whs(nw)"]')

data = []
for row in table_rows:
    data_row = []
    for col in row.xpath('//td'):
        data_row.append(col.text_content().strip())
    data.append(data_row)
df = pd.DataFrame(data, columns=column_headers)