'How to find a single <a href> from a multiple <p> tags and a div class
I have a question about beautifulsoup.
This is what my divs will look like.
<div class="guides__content-container">
<div class="row row-extra-small text-justify">
<div class="small-12 columns">
and then I'll have <p's> that will contain a href
<p>blalba <a href="test"></a> <a href="test1"><a></p>
<p>blalba <a href="test2"></a> <a href="test2"><a></p>
</div>
</div>
</div>
Unfortunately, I have no choice but to differentiate.
How can I get one href for each p if there is one?
I started like this.
from bs4 import BeautifulSoup
import requests
class Scrapping:
@staticmethod
def scrappingDrones(target):
req = requests.get(target)
soup = BeautifulSoup(req.text, "html.parser")
link = soup.find({"class" : "small-12 columns"})
print(link)
if __name__ == '__main__':
url = "h"
Scrapping.scrappingDrones(url)
Thanks in advance!
Solution 1:[1]
This can do the job. I have assumed that you want the first link from each p tag. Please let me know if I am wrong.
divs = soup.find("div", {"class": "small-12"})
paras = divs.find_all("p")
hrefs = []
for para in paras:
anchor = para.find("a")
hrefs.append(anchor.get("href"))
print(hrefs)
Output -
['test', 'test2']
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
