【问题标题】:Python Selenium - Change Url DatePython Selenium - 更改网址日期
【发布时间】:2021-06-09 10:40:26
【问题描述】:

我正在尝试更改以下网址中的入住和退房日期: https://www.booking.com/hotel/fr/le-leman.fr.html?label=gen173nr-1DCAEoggI46AdIM1gEaCyIAQGYAQ24ARjIAQzYAQPoAQGIAgGoAgS4AuGugoYGwAIB0gIkODQyNzI0MzUtNzlmZi00ZTY1LWJkNGUtZmQyMGIxMGE3NzJl2AIE4AIB;sid=1a1526834c348b388f7097913631c2e3;checkin=2021-06-09;checkout=2021-06-10;sig=v11C1wtOKO

我想过使用正则表达式规则来设置今天的日期:

d = datetime.date.fromordinal(datetime.date.today().toordinal()-0).strftime("%m/%d/%Y")
url = driver.current_url
regex = r"^\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$"
result = re.sub(regex, d, url, 0)
if result:
    print (result)

但它不起作用。

感谢您的关注。

PS : 对不起,我的英语不好..

【问题讨论】:

    标签: python selenium selenium-webdriver


    【解决方案1】:

    你不需要正则表达式,你可以简单地试试这个:

    import datetime
    
    checkin_date = datetime.date.fromordinal(datetime.date.today().toordinal()-0).strftime("%Y-%m-%d")
    checkout_date = datetime.date.fromordinal(datetime.date.today().toordinal()-0).strftime("%Y-%m-%d")
    url = "https://www.booking.com/hotel/fr/le-leman.fr.html?label=gen173nr-1DCAEoggI46AdIM1gEaCyIAQGYAQ24ARjIAQzYAQPoAQGIAgGoAgS4AuGugoYGwAIB0gIkODQyNzI0MzUtNzlmZi00ZTY1LWJkNGUtZmQyMGIxMGE3NzJl2AIE4AIB;sid=1a1526834c348b388f7097913631c2e3;checkin="+checkin_date+";checkout="+checkout_date+";sig=v11C1wtOKO"
    print(url)
    

    你可以给变量checkin_datecheckout_date你想要的值,然后简单地使用url。

    【讨论】:

      【解决方案2】:

      非常有帮助,我尝试了其他方法,仍然运行良好。

          # Selectionne la date d'aujourd'hui au format compatible booking
      d = datetime.date.fromordinal(datetime.date.today().toordinal()+1).strftime("%Y-%m-%d")
      # Selectionne la date d'aujourd'hui +n au format compatible booking
      d1 = datetime.date.fromordinal(datetime.date.today().toordinal()+2).strftime("%Y-%m-%d")
      # Variable pour préparer le changement sur booking
      dformat = 'checkin=' + d
      d1format = 'checkout=' + d1
      # Selectionne l'url actuelle
      url = driver.current_url
      #Remplace le text de la date par la nouvelle
      url = url.replace("checkin=2021-06-02", dformat)
      url = url.replace("checkout=2021-06-03", d1format)
      driver.get(url)
      

      它也有效,它比你的差一点,但它的功能;)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-22
        • 2017-01-05
        • 1970-01-01
        • 2014-10-02
        • 1970-01-01
        • 1970-01-01
        • 2011-09-17
        • 2021-08-19
        相关资源
        最近更新 更多