Home > database >  Get the all values of "a" tags
Get the all values of "a" tags

Time:02-13

I make a mini-project to reach a better level but I stuck here. Here is my code:

from bs4 import BeautifulSoup
import requests

html_from =  requests.get("http://www.muhfak.hacettepe.edu.tr/tr/duyurular")
html_from.encoding = "UTF-8"
html_text = html_from.text
soup = BeautifulSoup(html_text, 'html.parser')

titles = soup.find_all("div", class_="liste")

print(titles[0])

I have to add "[0]" after the "titles" because the whole part is in a one-length list. I can see all the sections that I need in HTML codes. Is there a way to put each value in different list values? When I make "titles[0].text", I can see the values I want. But "print(titles[0].a.text)" is not working and its output is only the latest value. How can I reach that values?

This is "titles[0].text" :

Yandal Başvurusu Hakkında 2022-01-05 15:00:00 2019/2020 - 2020/2021 Akademik Yıllarını Kapsayan Fakültemiz Faaliyet Raporu için Tıklayınız. 2021-12-14 08:00:00 13 Eylül 2021 Tarihinde Yapılan Mezuniyet Töreni Videosu için Tıklayınız. 2021-09-15 15:00:00 ...

This is "titles[0].text" : Yandal Başvurusu Hakkında

This is the part of the source and there are lots of "a" tags :

<script charset="utf-8" type="text/javascript">
        $(document).ready(function() {
            $('#duyurular_7').dataTable( {
                "sPaginationType": "full_numbers",
                "bStateSave": false,
                "bSort": false,
                "olanguage": {
                    "url": "//hu-iys.hacettepe.edu.tr/templates/template3/js/datatables_langs/tr.json"
                }
            } );
        } );
    </script>
<table  id="duyurular_7">
<thead>
<tr> <th>Başlık</th> </tr>
</thead>
<tbody>
<tr><td><a href="/tr/yandal_basvurusu_hakkinda-177">Yandal Başvurusu Hakkında</a> <span >2022-01-05 15:00:00</span></td></tr>
<tr><td><a href="/tr/20192020_20202021_akademik_yil-184">2019/2020 - 2020/2021 Akademik Yıllarını Kapsayan Fakültemiz  Faaliyet Raporu için Tıklayınız.
</a> <span >2021-12-14 08:00:00</span></td></tr>
<tr><td><a href="https://www.youtube.com/watch?v=k55WKZAfmU8">13 Eylül 2021 Tarihinde Yapılan Mezuniyet Töreni Videosu için Tıklayınız.</a> <span >2021-09-15 15:00:00</span></td></tr>
<tr><td><a href="https://universitem.hacettepe.edu.tr/hayatin-butun-renkleri-burada-2/">Fakültemiz Bölümlerinin Tanıtım Günleri İçin Tıklayınız.</a> <span >2021-08-06 10:00:00</span></td></tr>
<tr><td><a href="http://fs.hacettepe.edu.tr/muhfak/dosyalar/Duyurular/2020_2021_Guz_Yandal_sonuclari.pdf">2020-2021 Akademik Yılı Yandal Programı Sonuçları için tıklayınız.</a> <span >2020-10-02 11:00:00</span></td></tr>```


CodePudding user response:

To get all <a> tags under tag with you can use CSS selector ".liste a". For example:

import requests
from bs4 import BeautifulSoup

html_from = requests.get("http://www.muhfak.hacettepe.edu.tr/tr/duyurular")
html_from.encoding = "UTF-8"
html_text = html_from.text
soup = BeautifulSoup(html_text, "html.parser")

for a in soup.select(".liste a"):
    print(a.get_text(strip=True))
    print(a["href"])
    print()

Prints:

Yandal Başvurusu Hakkında
/tr/yandal_basvurusu_hakkinda-177

2019/2020 - 2020/2021 Akademik Yıllarını Kapsayan Fakültemiz  Faaliyet Raporu için Tıklayınız.
/tr/20192020_20202021_akademik_yil-184

13 Eylül 2021 Tarihinde Yapılan Mezuniyet Töreni Videosu için Tıklayınız.
https://www.youtube.com/watch?v=k55WKZAfmU8

Fakültemiz Bölümlerinin Tanıtım Günleri İçin Tıklayınız.
https://universitem.hacettepe.edu.tr/hayatin-butun-renkleri-burada-2/

...and so on.
  • Related