I try to run the below code but still can’t get the proper output in Excel with headers. Please help.
JavaScript
x
13
13
1
!pip install selenium
2
!apt-get update # to update ubuntu to correctly run apt install
3
!apt install chromium-chromedriver
4
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
5
import sys
6
sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')
7
from selenium import webdriver
8
chrome_options = webdriver.ChromeOptions()
9
chrome_options.add_argument('--headless')
10
chrome_options.add_argument('--no-sandbox')
11
chrome_options.add_argument('--disable-dev-shm-usage')
12
wd = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
13
JavaScript
1
12
12
1
lists = ["FBRX", "GNLN", "TISI"]
2
3
result=[]
4
5
for list in lists:
6
url = "https://finance.yahoo.com/quote/{list}/profile?p={list}"
7
wd.get(url.format(list=list))
8
add = wd.find_element_by_xpath('//*[@id="Col1-0-Profile-Proxy"]/section/section[1]/table/tbody').text
9
10
print(list,add)
11
result.append([list,add])
12
The intended format will be in the below pic.
Advertisement
Answer
If an Excel spreadsheet isn’t mandatory, then your “result” variable can be saved as a txt file in the same formatting in terms of rows and columns by including the following lines of code afterwards:
JavaScript
1
9
1
import numpy as np
2
import pandas as pd
3
# Mout Drive;
4
from google.colab import drive
5
myGoogleDrive = drive.mount('/content/drive', force_remount = True)
6
%cd "/content/drive/My Drive/Colab Notebooks"
7
fileName = 'InsertNameForFile' + '.txt'
8
np.savetxt(fileName, result, delimiter = ", ", fmt = '%s')
9
To read that saved txt file at a later point in time:
JavaScript
1
2
1
savedResultsFile = pd.read_csv(fileName, header = None).copy()
2