terrazo
terrazo

Reputation: 85

Python, Beautifullsoup - get href link

I have question, I try to download links from the site and it returns None. I don't know what I'm doing wrong ... Can someone please help me ?? THX...

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup

    
page = "https://mojmikolow.pl/informacje,0.html"
page = requests.get(page).content
data_entries = BeautifulSoup(page, "html.parser").find_all("section", {"class": "news"})

for data_entrie in data_entries:    
    get_link = data_entrie.get('href')
    print(get_link)
       

Upvotes: 1

Views: 178

Answers (2)

chitown88
chitown88

Reputation: 28565

You have to pull out the anchor tag <a> that contains the href:

import requests
from bs4 import BeautifulSoup
page = "https://mojmikolow.pl/informacje,0.html"
page = requests.get(page).content
data_entries = BeautifulSoup(page, "html.parser").find_all("section", {"class": "news"})
    
for data_entrie in data_entries:    
    link_tag = data_entrie.find('a',href=True)
    get_link = link_tag.get('href')
    print(get_link)

Upvotes: 2

costaparas
costaparas

Reputation: 5237

You can use soup.find_all() to extract all the a (link) tags, and then get the value of the href attribute from each of them:

from bs4 import BeautifulSoup

import requests

page = "https://mojmikolow.pl/informacje,0.html"
page = requests.get(page).content
data_entries = BeautifulSoup(page, "html.parser").find_all("section", {"class": "news"})

for data_entry in data_entries:    
    links = data_entry.find_all("a", href=True)
    for link in links:
        print(link["href"])

Upvotes: 1

Related Questions