想要从多个for循环创建一个数据框

时间:2019-09-23 05:23:19

标签: python pandas selenium for-loop selenium-webdriver

我正在运行以下代码来创建一个DataFrame,需要更改打印语句,尝试了一堆东西,例如首先创建一个空白df,然后追加,但是该数据帧在每个循环中都会被覆盖。这是一个很新的东西,帮助非常感谢。 [这是理想的结果,需要它作为数据帧] [1]

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
driver = webdriver.Chrome(executable_path='chromedriver')
catlink=['https://www.daraz.com.bd/camera-lenses','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")
            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")))

        except (selenium.common.exceptions.NoSuchElementException):
            print('mainlink:'+str(link)+str(driver.title) +':No Banners')
            continue ```


  [1]: https://i.stack.imgur.com/m5ICz.png

2 个答案:

答案 0 :(得分:0)

您可以.mat-flat-button { background-color: grey; border-radius: 0px; } 进入列表中的循环值并最后调用 function Square(props) { return ( <button className="square" onClick={props.onClick}> {props.value}, {props.style}, </button> ); } class Board extends React.Component { constructor(props) { super(props); this.state = { backgroundColor: 'gray'; squares: Array(9).fill(null), xIsNext: true, }; } handleClick(i) { const squares = this.state.squares.slice(); const { containerStyle } = styles; squares[i] = this.state.xIsNext ? 'X' : 'O'; backgroundColor = this.state.xIsNext ? 'blue' : 'red'; this.setState({ backgroundColor: 'someNewColor', squares: squares, xIsNext: !this.state.xIsNext, }); } renderSquare(i) { return ( <Square value={this.state.squares[i]} color={this.state.backgroundColor} onClick={() => this.handleClick(i)} /> ); } 构造函数:

append

如果需要在DataFrame中添加3列,请附加元组:

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'])

答案 1 :(得分:0)

将值的元组(这是数据框的一行)附加到循环的url中,并在结尾处创建数据框

urls = []
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")))
                urls.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')
            urls.append((str(link)+str(driver.title), ':No Banners'))
            continue 

result = pd.DataFrame(urls, columns=["mainlink", "banner"])