【问题标题】:how to print urls in order using for in range in python如何在python中使用for in range按顺序打印url
【发布时间】:2022-01-11 08:22:55
【问题描述】:

我打算一一打开excel文件中的url

df = pd.read_excel("file", sheet_name="Sheet1"):

for i in range(0, len(df)):
    driver.get()

这是我迄今为止搜索过的。如果您提供更多提示,我将不胜感激。 干杯!

【问题讨论】:

  • 您是否考虑过改用 Openpyxl?使用它可以很容易地达到您想要的结果。

标签: python excel pandas selenium


【解决方案1】:

假设您的电子表格在“Sheet1”上有一个列标题(例如 URL),那么:

import pandas
import requests

df = pandas.read_excel('file.xlsm', sheet_name='Sheet1')

for url in df['URL']:
    requests.get(url)
    # process the response here

【讨论】:

    【解决方案2】:

    我会这样做:

    import os
    import time
    
    import pandas as pd
    from selenium import webdriver
    
    import chromedriver_autoinstaller
    
    chromedriver_autoinstaller.install()
    
    options = webdriver.ChromeOptions()
    driver_path = r'D:\\Automation\\chromedriver.exe'
    driver = webdriver.Chrome(options = options, executable_path=driver_path)
    driver.maximize_window()
    
    cwd = os.getcwd()
    df = pd.read_csv(cwd + "/csvfile_1.csv")
    for index, row in df.iterrows():
        driver.get(row['URL'])
        time.sleep(5)
    

    这就是我的 CSV 的样子:

    time.sleep(5) 用于可视化目的,您可以在测试此代码后将其删除。

    【讨论】:

    • @hyunseo2 我可以得到一些更新吗?
    猜你喜欢
    • 1970-01-01
    • 2023-03-20
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 2011-11-09
    • 1970-01-01
    相关资源
    最近更新 更多