简体   繁体   中英

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!

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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM