Hack Forums
[Python] Beautiful Soup finding href - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Beautiful Soup finding href (/showthread.php?tid=5370621)



Beautiful Soup finding href - IGotTheKeys - Aug 10, 2016

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)


* - zmetra - Aug 10, 2016

(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




* - Madathf - Aug 13, 2016

PHP Code

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

🤝


* - SIM-1866423 - Aug 13, 2016

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/).


* - Goodies - Aug 13, 2016

(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.