Pass different values to a string without repeating the same

0

I need to pass different values to the url, as you can see in the example, first I pass the values contained in dt_3, the problem is that I need to pass the rest of the values to all the url, that is, dt_7 and dt_31.

How can I simplify the process without having to repeat the urls for dt_7 and dt_31 and then all the urls should be contained in a list, the urls do not have the same format.

Thanks

    from datetime import date, timedelta

    dt_3 = date.today() + timedelta(3)
    dt_7 = date.today() + timedelta(7)
    dt_31 = date.today() + timedelta(31)

    Salta_Centro= 'https://www.rentalcars.com/SearchResults.do?country=Argentina&doYear={0}&doFiltering=true \
                    &dropFtsSearch=L&doDay={1}&searchType=allareasgeosearch&filterFrom=0&puMonth={2}&dropFtsInput=salta&dropCountry=Argentina \
                    &dropLongitude=-65.4167&puDay={3}&dropFtsLocationSearch=161&puHour=10&dropFtsEntry=22776&enabler=&distance=10 \
                    &dropFtsLocationName=Salta&dropCountryCode=&doMinute=0&countryCode=&puYear={4}&locationName=&puMinute=0&ftsInput=salta \
                    &ftsLocationName=Salta&ftsSearch=L&location=161&doMonth={5}&reducedCategory=medium&filterAdditionalInfo=&advSearch=&exSuppliers=&ordering=price' \
                    .format(dt_3.year,dt_3.day,date.today().month,date.today().day,date.today().year,dt_3.today().month,).replace(' ','')

    Mendoza_Centro = 'https://www.rentalcars.com/SearchResults.do?country=Argentina&doYear={0}&doFiltering=true \
                    &dropFtsSearch=L&doDay={1}&searchType=allareasgeosearch&filterFrom=0&puMonth={2}&dropFtsInput=Mendonza&dropCountry=Argentina \
                    &dropLongitude=-68.843&puDay={3}&dropFtsLocationSearch=106&puHour=10&dropFtsEntry=22776&enabler=&distance=10 \
                    &dropFtsType=C&ftsAutocomplete=Mendoza%2c+Mendoza%2c+Argentina&driversAge=30&dropFtsAutocomplete=Mendoza%2c+Mendoza%2c+Argentina \
                    &dropFtsLocationName=Mendoza&dropCountryCode=&doMinute=0&countryCode=&puYear={4}&locationName=&puMinute=0&ftsInput=Mendoza \

                    &ftsLocationName=Mendoza&ftsSearch=L&location=106&doMonth={5}&reducedCategory=medium&filterAdditionalInfo=&advSearch=&exSuppliers=&ordering=price' \
                    .format(dt_3.year,dt_3.day,date.today().month,date.today().day,date.today().year,dt_3.today().month,).replace(' ','')


    Tucuman_Centro = 'https://www.rentalcars.com/SearchResults.do?country=Argentina&doYear={0}&doFiltering=true \
                    &dropFtsSearch=L&doDay={1}&searchType=allareasgeosearch&filterFrom=0&puMonth={2}&dropFtsInput=tucuman&dropCountry=Argentina \
                    &puDay={3}&dropFtsLocationSearch=-1&puHour=10&dropFtsEntry=22776&enabler=&distance=10 \
                    &dropFtsLocationName=Tucum%C3%A1n&dropCountryCode=&doMinute=0&countryCode=&puYear={4}&locationName=&puMinute=0&ftsInput=tucuman \
                    &ftsLocationName=Tucum%C3%A1n&ftsSearch=L&location=-1&doMonth={5}&reducedCategory=medium&filterAdditionalInfo=&advSearch=&exSuppliers=&ordering=price' \
                    .format(dt_3.year,dt_3.day,date.today().month,date.today().day,date.today().year,dt_3.today().month,).replace(' ','')


    buenos_aires = 'https://www.rentalcars.com/SearchResults.do?country=Argentina&doYear={0}&doFiltering=true \
                    &dropFtsSearch=L&doDay={1}&searchType=allareasgeosearch&filterFrom=0&puMonth={2}&dropFtsInput=buenos+aires&dropCountry=Argentina \
                    &dropLongitude=-58.3816&puDay={3}&dropFtsLocationSearch=51&puHour=10&dropFtsEntry=22776&enabler=&distance=10 \
                    &dropFtsLocationName=Buenos+Aires&dropCountryCode=&doMinute=0&countryCode=&puYear={4}&locationName=&puMinute=0&ftsInput=buenos+aires \
                    &ftsLocationName=Buenos+Aires&ftsSearch=L&location=51&doMonth={5}&reducedCategory=medium&filterAdditionalInfo=&advSearch=&exSuppliers=&ordering=price' \
                    .format(dt_3.year,dt_3.day,date.today().month,date.today().day,date.today().year,dt_3.today().month,).replace(' ','')


urlList = [Salta_Centro,Mendoza_Centro,Tucuman_Centro,buenos_aires]
    
asked by Sebastian 17.10.2018 в 15:43
source

1 answer

2

First of all, although it is not what you are asking, beware of the chain that occupies several lines, you have done the following (I give a more simplified example):

cadena = 'Esto es una cadena larga \
          que ocupa varias lineas'

You have included a \ at the end of the line to indicate that it continues for the next, but you have not taken into account that all the spaces before the word "that" will also be part of a chain. This is:

>>> cadena
'Esto es una cadena larga           que ocupa varias lineas'

We do not want this, especially in your case where the string is a URL, because you are introducing unwanted spaces in it.

To solve this, it is worth remembering a couple of tricks:

  • Two literal strings that appear followed in the code are concatenated by python in one. That is, if you do:

    prueba = "Esto es una "   "prueba"   "?"
    

    will be the same as if you had done:

    prueba = "Esto es una prueba?"
    
  • An expression in parentheses can be split into several lines without the need to put \ at the end of each.

  • Putting together both tricks, the previous assignment could look like this:

    cadena = ("Esto es una cadena larga "
              "que ocupa varias lineas")
    

    And now yes, let's go with your question.

    The string with the huge URL is quite repetitive, but it is not 100% identical for all cases. I tried to search "by eye" which parts are the same and which are different, but in the end it is quite complex and I preferred to show you what the general method would be, instead of trying to solve it for this specific problem.

    The idea is to have a string "template" that has all the common part, and that instead use placeholders as {1} , {2} , etc in the parts that have to be replaced. Like what you have done for the data of the date, but also extending it to the data of the city.

    Something like this:

    plantilla = ("https://www.rentalcars.com/SearchResults.do?"
                 "country={pais}&doYear={año}&doFiltering=true"
                 "&dropFtsSearch=L&doDay={dia}&searchType=allareasgeosearch&"
                 "filterFrom=0&puMonth={mes}&dropFtsInput={input}&"
                 "dropCountry={pais}"
                 "...")
    

    In a list you would have a series of dictionaries with the data to be replaced in the placeholders . For example:

    casos = [
      {  "pais": "Argentina",
         "input": "salta",
         "location": "Salta"},
      {  "pais": "Argentina",
         "input": "mendoza",
         "location": "Mendoza"},
      ]
    

    Finally a loop that goes through the list casos can use format on the plantilla providing a dictionary with the fields to fill. You can extend the dictionary that comes out of casos with the data of the date. For example:

    for datos in casos:
      datos.update({"año": 2018, "mes": 10, "dia": 17})
      print(plantilla.format(**datos))
    

    This code, with the plantilla and the data contained in the casos that I set before by way of example, would print the following strings (instead of printing them, you can naturally save them in a list):

    https://www.rentalcars.com/SearchResults.do?country=Argentina&doYear=2018&doFiltering=true&dropFtsSearch=L&doDay=17&searchType=allareasgeosearch&filterFrom=0&puMonth=10&dropFtsInput=salta&dropCountry=Argentina...
    https://www.rentalcars.com/SearchResults.do?country=Argentina&doYear=2018&doFiltering=true&dropFtsSearch=L&doDay=17&searchType=allareasgeosearch&filterFrom=0&puMonth=10&dropFtsInput=mendoza&dropCountry=Argentina...
    

    As I said, this is not a direct solution, but only the mechanism. You have to adapt it to your case with the correct template and the appropriate data dictionary for each case.

        
    answered by 17.10.2018 / 17:04
    source