Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Beautiful Soup finding href

Hey, im trying to retrieve the 64 from this <a> tag.Could anyone tell me how to use the soup.find function in regards to finding a href and a class(in this case the "temp" class )

"<a href="THELINKWOULDGOHEREBUTICANTPOSTIT">London, United Kingdom<span class="temp">64&deg;</span><span class="icon i-33-s"></span></a>
"

Heres what ive tried:

data1 = [link['href'] for link in soup.findAll("a", {"class": "temp"})]
data1= soup.find('a', attrs={'class' : 'temp'}).find('a', href=True)
(Aug 10, 2016 04:22 PM)IGotTheKeys Wrote:  Hey, im trying to retrieve the 64 from this <a> tag.Could anyone tell me how to use the soup.find function in regards to finding a href and a class(in this case the "temp" class )

"<a href="THELINKWOULDGOHEREBUTICANTPOSTIT">London, United Kingdom<span class="temp">64&deg;</span><span class="icon i-33-s"></span></a>
"

Heres what ive tried:

data1 = [link['href'] for link in soup.findAll("a", {"class": "temp"})]
data1= soup.find('a', attrs={'class' : 'temp'}).find('a', href=True)

I'm not familiar with beautifulsoup, but you can use the re module.

Code
# Removing the ''.join will output a tuple.
for data in re.findall('<a href="(.*?)">|<span class="temp">(\d+)&deg;', html):
    print(''.join(data))

Output:
Quote:THELINKWOULDGOHEREBUTICANTPOSTIT
64

PHP Code

<?php 
temp 
= soup.findAll('span', attrs = {"class" : "temp"})[0].string 

🤝
PHP Code

<?php 
import bs4


soup 
= bs4.BeautifulSoup('<a href="THELINKWOULDGOHEREBUTICANTPOSTIT">London, United Kingdom<span class="temp">64&deg;</span><span class="icon i-33-s"></span></a>',"html.parser")
print  (
soup.find_all("span", class_="temp" )[0].contents[0]) 

Py3 not use findAll anymore be careful(https://www.crummy.com/software/BeautifulSoup/bs4/doc/).
(Aug 10, 2016 05:19 PM)zmetra Wrote:  I'm not familiar with beautifulsoup, but you can use the re module.

Code
# Removing the ''.join will output a tuple.
for data in re.findall('<a href="(.*?)">|<span class="temp">(\d+)&deg;', html):
    print(''.join(data))

Output:

I love regex as much as the next guy, assuming the next guy also has nightmares about regex, but bs4 is the way to go for any html parsing project.
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by A +Feb 8, 2021 02:10 AM 8 206 Feb 8, 2021 02:10 AM
Last Post: vorticalbox
[Python] by SIM-1866423Apr 16, 2020 05:35 PM 9 233 Apr 16, 2020 05:35 PM
Last Post: Nachfolger
[Python] by SIM-574702Sep 11, 2017 01:45 AM 3 164 Sep 11, 2017 01:45 AM
Last Post: vorticalbox