Вы можете append
в значениях цикла в списке и последнем вызове. DataFrame
Конструктор:
L = []
for link in catlink:
driver.get(link)
time.sleep(10)
for i in range(0,8):
try:
driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/div[1]/div[1]/div/div[3]').click()
time.sleep(4)
i+=1
elements = driver.find_elements_by_css_selector("div > div:nth-child(2) > div.c2xMr_ > div.ant-carousel > div > div.slick-list>* a")
for element in elements:
# want to create a dataframe from these two blocks: check the screen shot for the desired result
print('mainlink:'+str(link)+str(driver.title) +':'+ str(element.get_attribute("href")))
L.append(str(link)+str(driver.title) +':'+ str(element.get_attribute("href")))
except (selenium.common.exceptions.NoSuchElementException):
print('mainlink:'+str(link)+str(driver.title) +':No Banners')
continue ```
df = pd.DataFrame(L, columns=['mainlink'])
При необходимости 3 столбца в DataFrame
добавить кортежи:
L = []
for link in catlink:
driver.get(link)
time.sleep(10)
for i in range(0,8):
try:
driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/div[1]/div[1]/div/div[3]').click()
time.sleep(4)
i+=1
elements = driver.find_elements_by_css_selector("div > div:nth-child(2) > div.c2xMr_ > div.ant-carousel > div > div.slick-list>* a")
for element in elements:
# want to create a dataframe from these two blocks: check the screen shot for the desired result
print('mainlink:'+str(link)+str(driver.title) +':'+ str(element.get_attribute("href")))
L.append((str(link), str(driver.title), str(element.get_attribute("href")))
except (selenium.common.exceptions.NoSuchElementException):
print('mainlink:'+str(link)+str(driver.title) +':No Banners')
continue ```
df = pd.DataFrame(L, columns=['link','title','href'])