Split URL at – With Python

Question:

Does anyone know how I can extract the end 6 characters in a absoloute URL e.g

/es/ideas-de-trading-y-noticias/el-ibex-35-insiste-en-buscar-los-7900-puntos-a-la-espera-de-las--221104

This is not a typical URL sometimetimes it ends -221104

Also, is there a way to turn 221104 into the date 04 11 2022 easily?

Thanks in advance

Mark

Asked By: Mark Leach

||

Answers:

Assuming that -- will only be there as it is in the url you posted, you can do something as follows:

You can split the URL at -- & extract the element

a = 'https://www.ig.com/es/ideas-de-trading-y-noticias/el-ibex-35-insiste-en-buscar-los-7900-puntos-a-la-espera-de-las--221104'

desired_value = a.split('--')[1]

& to convert:

from datetime import datetime
converted_date = datetime.strptime(desired_value , "%y%m%d")
formatted_date = datetime.strftime(converted_date, "%d %m %Y")
Answered By: some_programmer

If the url always has this structure (that is it has the date at the end after a -- and only has -- once), you can get the date with:

str_date = str(url).split("--")[1]

Relaxing the assumption to have only one --, we can have the code working by just taking the last element of the splitted list (again assuming the date is always at the end):

str_date = str(url).split("--")[-1]

(Thanks to @The Myth for pointing that out)

To convert the obtained date into a datetime.date object and get it in the format you want:

from datetime import datetime
datetime_date = datetime.strptime(str_date, "%y%m%d")
formatted_date = datetime_date.strftime("%d %m %Y")
print(formatted_date)  # 04 11 2022

Docs:

Answered By: mattiatantardini

Taking into consideration the date is constant in the format yy-mm-dd. You can split the URL by:

url = "https://www.ig.com/es/ideas-de-trading-y-noticias/el-ibex-35-insiste-en-buscar-los-7900-puntos-a-la-espera-de-las--221104"
time = url[-6:] # Gets last 6 values

To convert yy-mm-dd into dd mm yy we will use the DateTime module:

import datetime as dt
new_time = dt.datetime.strptime(time, '%y%m%d') # Converts your date into datetime using the format
format_time = dt.datetime.strftime(new_time, '%d-%m-%Y') # Format
print(format_time)

The whole code looks like this:

url = "https://www.ig.com/es/ideas-de-trading-y-noticias/el-ibex-35-insiste-en-buscar-los-7900-puntos-a-la-espera-de-las--221104"
time = url[-6:] # Gets last 6 values

import datetime as dt
new_time = dt.datetime.strptime(time, '%y%m%d') # Converts your date into datetime using the format
format_time = dt.datetime.strftime(new_time, '%d %m %Y') # Format
print(format_time)

Learn more about datetime

Answered By: The Myth

You can use python built-in split function.

date = url.split("--")[1]

It gives us 221104

then you can modify the string by rearranging it

date_string = f"{date[4:6]} {date[2:4]} {date[0:2]}"

this gives us 04 11 22

Answered By: Jimpsoni

You should use the datetime module for parsing strings into datetimes, like so.

from datetime import datetime

url = 'https://www.ig.com/es/ideas-de-trading-y-noticias/el-ibex-35-insiste-en-buscar-los-7900-puntos-a-la-espera-de-las--221104'

datetime_string = url.split('--')[1]

date = datetime.strptime(datetime_string, '%y%m%d')

print(f"{date.day} {date.month} {date.year}")

the %y%m%d text tells the strptime method that the string of ‘221104’ is formatted in the way that the first two letters are the year, the next two are the month, and the final two are the day.

Here is a link to the documentation on using this method:
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

Answered By: Ryan Nygard
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.