1

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)
       
terrazo
  • 85
  • 1
  • 10
  • Duplicate of https://stackoverflow.com/questions/1080411/retrieve-links-from-web-page-using-python-and-beautifulsoup – hahaboki Jan 29 '21 at 10:33

2 Answers2

2

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"])
costaparas
  • 5,047
  • 11
  • 16
  • 26
2

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)
hahaboki
  • 103
  • 4
chitown88
  • 27,527
  • 4
  • 30
  • 59