I
I
irsby2020-11-27 15:55:36
Python
irsby, 2020-11-27 15:55:36

How to parse article number from hyperlink?

I have the following hyperlink with the article number at the end of the url:

<a href="https://example.com/?p=5144" rel="bookmark"> example </a>

I am creating a telegram bot that compares the number of the last article with the one that was published, and if the number is higher, then the bot sends a notification about a new article.

How to extract article number from url? I use Python and beautifulsoup.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2020-11-27
@irsby

In any convenient way
Split

url = 'https://example.com/?p=5144'
page = url.split('?p=')[-1]

replacement
url = 'https://example.com/?p=5144'
page = url.replace('https://example.com/?p=', '')

Regex
import re
url = 'https://example.com/?p=5144'
page = re.search(r'\?p=(\d+)', url).group(1)

Z
zyusifov11, 2020-12-01
@zyusifov11

url = soup.find("a", class_="класс тега").get("href")
page = url.replace('https://example.com/?p=', '')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question