Extract part of href link using beautiful soup

Viewed 98

I'm new to python requests and beautiful soup, I was trying to create a script to automatically login and logout, but I need the "_wpnonce" value to send a valid post request. I am wondering if there is a way to extract the "_wpnonce" value from this link using beautiful soup, any help is appreciated.

<a href="https://www.example.com/wp-login.php?action=logout&amp;_wpnonce=fc789123fe&redirect_to=%2Fuser%2Flogout">Sign Out</a>

The extracted value would be "fc789123fe"

2 Answers

You can use urllib.parse to parse the URL to components (the correct official way):

For example:

txt = '''<a href="https://www.example.com/wp-login.php?action=logout&amp;_wpnonce=fc789123fe&redirect_to=%2Fuser%2Flogout">Sign Out</a>'''

from bs4 import BeautifulSoup
from urllib.parse import parse_qs, urlparse

soup = BeautifulSoup(txt, 'html.parser')

href = soup.select_one('a')['href']
parameters = parse_qs(urlparse(href).query)

print( parameters['_wpnonce'][0] )

Prints:

fc789123fe

To "cheat" a little bit, you can use re module:

print( re.search(r'_wpnonce=([a-f0-9]+)', soup.select_one('a')['href']).group(1) )

EDIT: To find tag <a> with text Sign Out you can do:

href = soup.find('a', text='Sign Out')['href']

this should work

tag = r'<a href="https://www.example.com/wp-login.phpaction=logout&amp;_wpnonce=fc789123fe&redirect_to=%2Fuser%2Flogout">Sign Out</a>'
tag = tag.split("_wpnonce=")[1]
tag = tag.split("&")[0]
print(tag)

output

fc789123fe
Related