I try to get articles under same date with different page, there are three' Corp,FIG,SSA', I need to click one and back and click the other, but the xpath for element is same, so I am wondering is there some 'smart' way to do that instead of copying again and again?
And I also want the website be back if there is no article in the page, should I use 'try'?
Surprisingly, I got the results twice in the csv file, like aabb... no idea why
driver.get('https://www.globalcapital.com/Asia/Bonds')
Corp = driver.find_element_by_link_text("Corp")
Corp.click()
driver.implicitly_wait(10)
links=[link.get_attribute('href') for link in driver.find_elements_by_xpath("//div[contains(text(),'28 Jan 2021')]/preceding::a[2]")]
titles = [link.text for link in driver.find_elements_by_xpath("//div[contains(text(),'28 Jan 2021')]/preceding-sibling::h3/a")]
for link in links:
for title in titles:
dataframe = pd.DataFrame({'col1':title,'col2':link},index=[0])
dataframe.to_csv('hi.csv',mode='a+',header=False,index=False,encoding='utf-8-sig')
driver.back()
FIG = driver.find_element_by_link_text("FIG")
FIG.click()
driver.implicitly_wait(10)
links=[link.get_attribute('href') for link in driver.find_elements_by_xpath("//div[contains(text(),'28 Jan 2021')]/preceding::a[2]")]
titles = [link.text for link in driver.find_elements_by_xpath("//div[contains(text(),'28 Jan 2021')]/preceding-sibling::h3/a")]
for link in links:
for title in titles:
dataframe = pd.DataFrame({'col1':title,'col2':link},index=[0])
dataframe.to_csv('hi.csv',mode='a+',header=False,index=False,encoding='utf-8-sig')
driver.back()
SSA = driver.find_element_by_link_text("SSA")
SSA.click()
driver.implicitly_wait(10)