使用Python自动下载DHL CSV转储

使用Python自动下载DHL CSV转储,python,python-3.x,Python,Python 3.x,我有一个自动化的邮件发送过程与数据从DHL的要求。目前我们正在做的是: 我们有一个DHL帐户,有人必须手动登录该帐户,下载包含订单跟踪详细信息的CSV转储文件,然后将其上载到服务器,从这些文件中导入数据并进行处理 所以我想把整个过程自动化,这样只需要最少的人工干预 1) 我们是否可以自动化DHL的下载过程 注意:我正在使用Python您可以将Selenium用于Python。Selenium是一个自动化浏览器会话的包。您可以使用Selenium模拟鼠标单击和其他操作。 要安装: pip ins

我有一个自动化的邮件发送过程与数据从DHL的要求。目前我们正在做的是: 我们有一个DHL帐户,有人必须手动登录该帐户,下载包含订单跟踪详细信息的CSV转储文件,然后将其上载到服务器,从这些文件中导入数据并进行处理

所以我想把整个过程自动化,这样只需要最少的人工干预

1) 我们是否可以自动化DHL的下载过程


注意:我正在使用Python

您可以将Selenium用于Python。Selenium是一个自动化浏览器会话的包。您可以使用Selenium模拟鼠标单击和其他操作。 要安装:

 pip install selenium
您还必须为您喜欢使用的浏览器安装webdriver。

确保您使用的浏览器版本是最新的

Selenium文档:


因为您处理的是密码和敏感数据,所以我不包括代码。

您可以使用Selenium for Python。Selenium是一个自动化浏览器会话的包。您可以使用Selenium模拟鼠标单击和其他操作。 要安装:

 pip install selenium
您还必须为您喜欢使用的浏览器安装webdriver。

确保您使用的浏览器版本是最新的

Selenium文档:


由于您处理的是密码和敏感数据,因此我不包括代码。

我将从寻找更方便使用代码访问的内容开始

在谷歌搜索“dhl订单跟踪api”时,可以得到:

作为第一个结果,它看起来很有用,并公开了相当多的功能


然后,您需要弄清楚如何发出“RESTful”请求,这里有类似的答案,如果您搜索“python教程rest客户端”之类的内容,那么internet上有很多教程,这些内容指向类似于

的文章,我将从寻找更方便使用代码访问的内容开始

在谷歌搜索“dhl订单跟踪api”时,可以得到:

作为第一个结果,它看起来很有用,并公开了相当多的功能

然后,您需要弄清楚如何发出“RESTful”请求,这里有类似的答案,如果您搜索“python教程rest客户端”之类的内容,internet上有很多教程,这些内容指向类似的文章
  • 登录和下载
  • 您可以使用selenium自动化下载过程。下面是自动化任何登录过程和从网页下载项目的示例代码。由于需求不具体,我将使用通用用例并解释如何使用python自动化登录和下载过程

    # Libraries - selenium for scraping and time for delay
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    import time
    
    chromeOptions = webdriver.ChromeOptions()
    prefs = {"download.default_directory" : "Path to the directory to store downloaded files"}
    chromeOptions.add_experimental_option("prefs",prefs)
    chromedriver = r"Path to the directory where chrome driver is stored"
    browser = webdriver.Chrome(executable_path=chromedriver, chrome_options=chromeOptions)
    
    # To maximize the browser window 
    browser.maximize_window() 
    
    # web link for login page
    browser.get('login page link') 
    
    time.sleep(3) # wait for the page to load
    
    # Enter your user name and password here. 
    username = "YOUR USER NAME"
    password = "YOUR PASSWORD"
    
    # username send 
    # you can find xpath to the element in developer option of the chrome
    # referance answer "[https://stackoverflow.com/questions/3030487/is-there-a-way-to-get-the-xpath-in-google-chrome][1]"
    a = browser.find_element_by_xpath("xpath to username text box") # find the xpath for username text box and replace inside the quotes
    a.send_keys(username) # pass your username
    
    # password send 
    b = browser.find_element_by_xpath("xpath to password text box") # find the xpath for password text box and replace inside the quotes
    b.send_keys(password) # pass your password
    
    # submit button clicked 
    browser.find_element_by_xpath("xpath to submit button").click() # find the xpath for submit or login button and replace inside the quotes
    
    time.sleep(2) # wait for login to complete
    
    print('Login Successful') # if there is no error you will see "Login Successful" message
    # Navigate to the menu or any section using it's xpath and you can click using click() function
    browser.find_element_by_xpath("x-path of the section/menu").click()
    
    time.sleep(1)
    
    # download file
    browser.find_element_by_xpath("xpath of the download file button").click()
    time.sleep(1)
    
    # close browser window after successful completion of the process.
    browser.close()  
    
    通过这种方式,您可以自动完成登录和下载过程

    • 邮件自动化
    要使用smtplib模块实现邮件自动化,请浏览此文档“”

    • 过程自动化(调度)
    要每天自动化整个过程,请为这两个任务创建一个cron作业。请参阅python crontab模块。文件:

    通过使用selenium、smtplib和python crontab,您可以在最少或不需要手动干预的情况下自动化整个过程。

    • 登录和下载
    您可以使用selenium自动化下载过程。下面是自动化任何登录过程和从网页下载项目的示例代码。由于需求不具体,我将使用通用用例并解释如何使用python自动化登录和下载过程

    # Libraries - selenium for scraping and time for delay
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    import time
    
    chromeOptions = webdriver.ChromeOptions()
    prefs = {"download.default_directory" : "Path to the directory to store downloaded files"}
    chromeOptions.add_experimental_option("prefs",prefs)
    chromedriver = r"Path to the directory where chrome driver is stored"
    browser = webdriver.Chrome(executable_path=chromedriver, chrome_options=chromeOptions)
    
    # To maximize the browser window 
    browser.maximize_window() 
    
    # web link for login page
    browser.get('login page link') 
    
    time.sleep(3) # wait for the page to load
    
    # Enter your user name and password here. 
    username = "YOUR USER NAME"
    password = "YOUR PASSWORD"
    
    # username send 
    # you can find xpath to the element in developer option of the chrome
    # referance answer "[https://stackoverflow.com/questions/3030487/is-there-a-way-to-get-the-xpath-in-google-chrome][1]"
    a = browser.find_element_by_xpath("xpath to username text box") # find the xpath for username text box and replace inside the quotes
    a.send_keys(username) # pass your username
    
    # password send 
    b = browser.find_element_by_xpath("xpath to password text box") # find the xpath for password text box and replace inside the quotes
    b.send_keys(password) # pass your password
    
    # submit button clicked 
    browser.find_element_by_xpath("xpath to submit button").click() # find the xpath for submit or login button and replace inside the quotes
    
    time.sleep(2) # wait for login to complete
    
    print('Login Successful') # if there is no error you will see "Login Successful" message
    # Navigate to the menu or any section using it's xpath and you can click using click() function
    browser.find_element_by_xpath("x-path of the section/menu").click()
    
    time.sleep(1)
    
    # download file
    browser.find_element_by_xpath("xpath of the download file button").click()
    time.sleep(1)
    
    # close browser window after successful completion of the process.
    browser.close()  
    
    通过这种方式,您可以自动完成登录和下载过程

    • 邮件自动化
    要使用smtplib模块实现邮件自动化,请浏览此文档“”

    • 过程自动化(调度)
    要每天自动化整个过程,请为这两个任务创建一个cron作业。请参阅python crontab模块。文件:


    通过使用selenium、smtplib和python crontab,您可以在最少或不需要手动干预的情况下自动化整个过程。

    非常感谢您的详细回复,我将对此进行检查,并将很快更新线程。我建议这样做的原因(以及为什么这样的API很受欢迎)面向用户的网站往往会以破坏与之交互的代码的方式定期更改。以开发人员为中心的API趋向于对代码更加稳定和友好。i、 e.使用selenium(我已经使用过很多次的一个伟大项目)可能需要的代码是使用REST端点的10倍。非常感谢您的详细响应,我将对此进行检查,并将很快更新线程。我建议这样做的原因(以及为什么这样的API很受欢迎)面向用户的网站往往会以破坏与之交互的代码的方式定期更改。以开发人员为中心的API趋向于对代码更加稳定和友好。i、 e.使用像selenium这样的东西(我已经用过很多次了,这是一个很棒的项目)需要的代码可能是使用REST端点的10倍。selenium有什么帮助?我正在使用python,在下载之后,我必须处理该文件,并将该数据用于临时报告目的。因此,我认为这可能没有帮助。根据您的要求,以下是流程:1)Selenium打开DHL登录页面URL 2)自动发送密码和用户名等字段3)使用xPath找到csv下载按钮4)单击下载按钮5)您的程序使用os模块提取csv文件5)执行处理6)发送结果电子邮件处理后使用Python模块SMTPLib。使用Cronjob根据需要频繁运行整个流程。希望这有帮助,谢谢。硒能有什么帮助?我正在使用python,在下载之后,我必须处理该文件,并将该数据用于临时报告目的。所以我想这可能帮不上忙。按照你的要求,他