在Python中循环增量日期

时间:2017-03-16 23:38:50

标签: python selenium

我正在尝试创建一个循环,每次使用增量日期链接打开一个新的Chrome标签 - 例如:www.biguser.com/31-04-2015,然后打开www.biguser.com/01-05-2015 打开链接后,它会点击一个可下载的链接(使用Selenium),该链接具有标准的.csv格式。因此,我搜索了find_element _by_partial_link_text的链接,用于' csv'然后单击链接。

这是代码 -

from datetime import timedelta, date
import selenium.webdriver as webdriver
import selenium.webdriver.support.ui as ui
import time
import urllib2
import re
from BeautifulSoup import BeautifulSoup
from selenium.webdriver.common.keys import Keys

link_1 = "https://www.biguser.com/date=" #this is part 1 (prefix) of the link
link_2 = "&section=q" #this part comes after date is put in dd-mm-yyyy format

driver=webdriver.Chrome()
#loop for determining the increments in date
def daterange(start_date, end_date):
    for n in range(int ((end_date - start_date).days)):
        yield start_date + timedelta(n)

start_date = date(2013, 12, 31) #defining my start date
end_date = date(2015, 12, 31) #defining my end date

while True:
#loop begins
        for single_date in daterange(start_date, end_date):
            driver.get(link_1+single_date.strftime("%d-%m-%Y")+link_2) #opens the concatenated link
            driver.find_element_by_partial_link_text('csv').click() #finds 'csv' text and clicks on the link that contains it
            time.sleep(5) #waits for 5 seconds for everything to settle down
            driver.get("chrome://newtab/") #opens a new Chrome tab

以及我遇到的问题 可下载文件仅适用于WEEKDAYS,这意味着没有' csv'在页面上只是一条错误消息,上面写着"找不到指定日期的文件。尝试另一个约会。"每当代码遇到这个时,它就会退出程序。

我希望代码只需跳过"点击链接"如果链接不可用,则继续执行下一个日期。该页面没有href或标签。

PS:作为一个极端的初学者,我通过各种实验将这些代码放在一起(你可以看到我加载了太多的库;)

1 个答案:

答案 0 :(得分:0)

日期类有一个weekday()方法,可用于检查给定日期是否为工作日。我建议为if语句创建一个小帮助函数:

def is_weekday(date_object):
    # days 1-5 are weekdays, 6 and 7 are weekends
    return date.weekday(date_object) in range(1,6)

然后你可以检查:

if is_weekday(<your date here>):
  ...make the link...

如果还有其他因素,例如假期,您可能无法预测所有这些因素,因此我建议围绕失败的操作设置一个try / except块:

for single_date in daterange(start_date, end_date):
  try:
    driver.get(link_1+single_date.strftime("%d-%m-%Y")+link_2) #opens the concatenated link
    driver.find_element_by_partial_link_text('csv').click() #finds 'csv' text and clicks on the link that contains it
    time.sleep(5) #waits for 5 seconds for everything to settle down
    driver.get("chrome://newtab/") #opens a new Chrome tab
  except:
    continue  #this will return to the top of the loop and move on to the next link