我想使用for循环从列表创建DataFrame。但只有最后一个值附加到数据帧中。需要一个解决方案。我对这个话题很陌生。只是想解决个人问题。你知道吗
我可以用下面提供的代码打印所需的值。但当我试图将这些值附加到数据帧中时,这只是附加列表中的最后一个值。你知道吗
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import selenium.common.exceptions
import time
driver = webdriver.Chrome(executable_path='chromedriver')
catlink=['https://www.daraz.com.bd/small-kitchen-appliances/','https://www.daraz.com.bd/bedding-bath/']
for link in catlink:
driver.get(link)
time.sleep(10)
for i in range(0,8):
driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/div[1]/div[1]/div/div[3]').click()
i+=i
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:
print(driver.title)
print(element.get_attribute("href"))
import pandas as pd
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import selenium.common.exceptions
import time
driver = webdriver.Chrome(executable_path='chromedriver')
catlink=['https://www.daraz.com.bd/small-kitchen-appliances/','https://www.daraz.com.bd/bedding-bath/']
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")
title=[]
# hreflink=[]
for element in elements:
title.append({'title':driver.title,'link':element.get_attribute("href")})
df=pd.DataFrame(title)
except (selenium.common.exceptions.NoSuchElementException):
NoBanner=list()
NoBanner.append(driver.title)
continue ```
I tried this but now showing the expected result.
您正在为
for element in elements
循环的每次迭代创建一个新的数据帧,并将其分配给变量df
,从而重写df
的先前值。你知道吗相关问题 更多 >
编程相关推荐