1

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

1
  • You can use the datetime library to parse it as a date. Commented Nov 4, 2022 at 20:05

5 Answers 5

3

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

Sign up to request clarification or add additional context in comments.

Comments

2

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:

4 Comments

What if there's a double --? They also want the time converted.
Also %Y is the wrong format.
Got it, thanks! About the multiple --, I'm assuming there's only one due to the structure of the URL (I guess it may be related to some indexing of the underlying website data)
Fixed the code also in case multiple -- are present
1

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

2 Comments

Hi all, thank you so much for your help. When I use url.split("--")[1] i get the message IndexError: list index out of range. Why is that?
Don't go with splitting, I won't recommend it. Try my method of slicing the string. Do string[-6:] where string is your variable name.
1

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

4 Comments

Why are you using such an inefficient method? What about datetime???
Agree with @The Myth, it is a very manual solution. Moreover, it does not format the date as required
Also, the proposed split only works assuming only -- is present in the url
But the user specifically asked the string to be extracted from that url with that formating. I think this is simple and easy to read solution.
1

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")

3 Comments

There is no function datetime.strptime()
Also %Y is the wrong format.
Great, also what about formatting the time again?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.