I'm trying to save data in SSMS (SQL) but can't able to insert in DB.
The Error which I get - 'type' object is not subscriptable
Error Block:
try:
for record in records:
print(record)
cursor.execute(insert_statement, record)
except Exception as e:
cursor.rollback()
print(e)
print('Transaction Rolled Back')
else:
print('Record Inserted Sucessfully')
cursor.commit()
Conn.close()
finally:
if Conn.connected == 1:
print('Connection Closed')
Conn.close()
What I have tried:
Full Code:
try:
def GetLinkedin():
URLs_all_Page = GetURLsonPages()
for Linkedin_URL in URLs_all_Page:
driver.get(Linkedin_URL)
time.sleep(2)
page_source = driver.page_source
soup = BeautifulSoup(page_source, "html.parser")
ExeName = soup.find('dt', {'class':'flex align-items-center'}).find('span', {'class':'profile-topcard-person-entity__name t-24 t-black t-bold'})
soup_ExeName_1 = BeautifulSoup(ExeName.text.replace("\n", ""), 'html.parser')
soup_ExeName = BeautifulSoup(soup_ExeName_1.text.replace(" ", ""), 'html.parser')
#------------------------------------------------------------------------
if soup.find('dd', {'class':'profile-topcard__previous-positions flex mt3'}) is not None:
PreCompany = soup.find('dd', {'class':'profile-topcard__previous-positions flex mt3'}).find('span', {'class':'t-14 t-black t-bold'})
if PreCompany is None:
PreCompany = soup.find('dd', {'class':'profile-topcard__previous-positions flex mt3'}).find('span', {'class':'align-self-center'}).find('a', {'class':'li-i18n-linkto inverse-link-on-a-light-background t-14 t-black t-bold'})
soup_PreCompany = BeautifulSoup(PreCompany.text,'html.parser')
elif PreCompany is not None:
soup_PreCompany = BeautifulSoup(PreCompany.text,'html.parser')
elif soup.find('dd', {'class':'profile-topcard__previous-positions flex mt3'}) is None:
soup_PreCompany = "$No Data$"
#------------------------------------------------------------------------
NewTitle = soup.find('span', {'class':'align-self-center'}).find('span', {'class':'profile-topcard__summary-position-title'})
soup_NewTitle = BeautifulSoup(NewTitle.text,'html.parser')
#------------------------------------------------------------------------
Company = soup.find('span', {'class':'align-self-center'}).find('span', {'class':'t-14 t-black t-bold'})
if Company is None:
Company = soup.find('span', {'class':'align-self-center'}).find('a', {'class':'li-i18n-linkto inverse-link-on-a-light-background t-14 t-black t-bold'})
soup_Company = BeautifulSoup(Company.text, 'html.parser')
elif Company is not None:
soup_Company = BeautifulSoup(Company.text,'html.parser')
#------------------------------------------------------------------------
tenur = soup.find('span', {'class':'align-self-center'}).find('span', {'class':'profile-topcard__time-period-bullet'})
soup_tenur_1 = BeautifulSoup(tenur.text.replace("\n", ""),'html.parser')
soup_tenur = BeautifulSoup(soup_tenur_1.text.replace(" ", ""),'html.parser')
#------------------------------------------------------------------------
CRMIO = soup.find('button', {'class':'button--unstyled label-16dp color-black6 pv1 ph2 _crm-badge_5gr6fh'})
if CRMIO is None:
CRMIO = soup.find('button', {'class':'artdeco-button artdeco-button--muted artdeco-button--1 artdeco-button--tertiary ember-view label-16dp cursor-pointer color-black6 pv1 ph2 _crm-badge_5gr6fh'}).find('span', {'class':'artdeco-button__text'})
soup_CRMIO = BeautifulSoup(CRMIO.text.replace("\n", ""),'html.parser')
soup_CRM = BeautifulSoup(soup_CRMIO.text.replace(" ", ""),'html.parser')
elif CRMIO is not None:
soup_CRMIO = BeautifulSoup(CRMIO.text.replace("\n", ""),'html.parser')
soup_CRM = BeautifulSoup(soup_CRMIO.text.replace(" ", ""),'html.parser')
#------------------------------------------------------------------------
print (soup_ExeName,"|",soup_PreCompany,"|",soup_NewTitle,"|",soup_Company,"|",soup_tenur,"|",soup_CRM)
try:
records = [
[soup_ExeName, soup_PreCompany, soup_NewTitle, soup_Company, soup_tenur, soup_CRM]
]
DRIVER = 'SQL Server'
SERVER_NAME = 'SRIRAM\SQLEXPRESS'
DATABASE_NAME = 'DataSet'
Conn_string = f"""
Driver={{{DRIVER}}};
Server={SERVER_NAME};
Database={DATABASE_NAME};
Trust_Connection=yes;
"""
try:
Conn = odbc.connect(Conn_string)
except Exception as e:
print(e)
print('Task is Terminated')
sys.exit()
else:
cursor = Conn.cursor()
insert_statement = """
INSERT INTO RecordSet
VALUES (?, ?, ?, ?, ?, ?)
"""
try:
for record in records:
print(record)
cursor.execute(insert_statement, record)
except Exception as e:
cursor.rollback()
print(e)
print('Transaction Rolled Back')
else:
print('Record Inserted Sucessfully')
cursor.commit()
Conn.close()
finally:
if Conn.connected == 1:
print('Connection Closed')
Conn.close()
except Exception as e:
print(e)