【问题标题】:Iterate through CSV rows with Pandas, Perform Selenium Action使用 Pandas 遍历 CSV 行,执行 Selenium Action
【发布时间】:2019-08-01 03:22:32
【问题描述】:

我有一个使用 Pandas 创建的 CSV 文件。以下是以下代码的输出:

   test = pd.read_csv('order.csv', header=0)
   print(test.head())

      3  16258878505032
   0  3  16258876670024
   1  3  16258876899400
   2  3  16258876997704

我需要处理的唯一数据是第 2 列中的信息和第 3 列中的信息。这是采购订单数据,其中第 2 列代表“数量”,第 3 列代表“sku”。

我需要使用 selenium 将第 1 行第 2 列注入到输入字段中。我需要第 1 行,第 3 列并执行在网页上选择 sku 的操作。将商品添加到购物车并循环返回流程第 2 行、第 3 行等。

我知道如何编写 selenium 代码来执行基于 Web 的操作,但不知道如何编写 pandas/python 代码来一次遍历 CSV 文件一行以及如何调用这些值。我的逻辑如下。

read order.csv
    get quantity value and sku value for row (one row at the time)
        visit website, inject quantity value
        remain on website, select sku
        add to cart

        repeat loop until no more rows to process

感谢您的帮助。

【问题讨论】:

    标签: python pandas selenium


    【解决方案1】:

    首先在read_csv 中使用参数names 以避免将第一行数据转换为列名:

    test = pd.read_csv('order.csv', names=['quantity','sku'])
    print (test)
       quantity             sku
    0         3  16258878505032
    1         3  16258876670024
    2         3  16258876899400
    3         3  16258876997704
    

    因为使用 selenium 和 web 可以使用 DataFrame.iterrows 或其他循环解决方案:

    def func(x):
        q = x['quantity']
        sku = x['sku']
        print (q, sku)
        #add selenium code
    
    df.apply(func, axis=1)
    

    或者:

    for i, row in test.iterrows():
        q = row['quantity']
        sku = row['sku']
        print (q, sku)
        #add selenium code
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-23
      • 2019-10-26
      • 2021-12-16
      • 1970-01-01
      • 1970-01-01
      • 2018-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多