Parsing mutiple items using BeautifulSoup in Python











up vote
0
down vote

favorite












I'm trying to parse HTML from a website, where there are multiple elements having the same class ID. I can't seem to find a solution; I manage to get one item but not all of them.



Here's a bit of the HTML I'm trying to parse :



<h1>Synonymes travail</h1>
<div class="container-bloc1">
<strong> Nom</strong>
<br/>
-
<i><a class="lien2" href="/fr/accouchement.html"> accouchement </a></i>
:
<a class="lien3" href="/fr/gésine.html"> gésine</a>
<br/>
-
<i> <a class="lien2" href="/fr/action.html"> action </a></i>
:
<a class="lien3" href="/fr/activité.html"> activité</a>
,
<a class="lien3" href="/fr/labeur.html"> labeur</a>
</div>


In Python, I wrote it like this :



from bs4 import BeautifulSoup
import requests
import csv

source = requests.get("http://www.synonymes.net/fr/travail.html").text

soup = BeautifulSoup(source, "lxml")

for synonyme in soup.find_all("div", class_="container-bloc1"):
print(synonyme)

synonymesdumot = synonyme.find("a", class_="lien2").text
print(synonymesdumot)

for synonymesautres in synonyme.find_all("a", class_="lien3").text:
print(synonymesautres)


The first part is working, since there is only one "lien2" in the HTML file. I could do the same for "lien3" but I'd only get one item, and I want all of them.



What am I doing wrong here? Thanks for your help guys!










share|improve this question
























  • I'm not sure what happens if you use .text on a collection of multiple elements, as you are doing here. Does it work if you just loop with for synonymesautres in synonyme.find_all("a", class_="lien3"): and inside it do print(synonymesautres.text)?
    – Robin Zigmond
    Nov 12 at 9:21










  • It's working now… What a stupid mistake! Thanks for the help!
    – BeatJuice
    Nov 12 at 9:33















up vote
0
down vote

favorite












I'm trying to parse HTML from a website, where there are multiple elements having the same class ID. I can't seem to find a solution; I manage to get one item but not all of them.



Here's a bit of the HTML I'm trying to parse :



<h1>Synonymes travail</h1>
<div class="container-bloc1">
<strong> Nom</strong>
<br/>
-
<i><a class="lien2" href="/fr/accouchement.html"> accouchement </a></i>
:
<a class="lien3" href="/fr/gésine.html"> gésine</a>
<br/>
-
<i> <a class="lien2" href="/fr/action.html"> action </a></i>
:
<a class="lien3" href="/fr/activité.html"> activité</a>
,
<a class="lien3" href="/fr/labeur.html"> labeur</a>
</div>


In Python, I wrote it like this :



from bs4 import BeautifulSoup
import requests
import csv

source = requests.get("http://www.synonymes.net/fr/travail.html").text

soup = BeautifulSoup(source, "lxml")

for synonyme in soup.find_all("div", class_="container-bloc1"):
print(synonyme)

synonymesdumot = synonyme.find("a", class_="lien2").text
print(synonymesdumot)

for synonymesautres in synonyme.find_all("a", class_="lien3").text:
print(synonymesautres)


The first part is working, since there is only one "lien2" in the HTML file. I could do the same for "lien3" but I'd only get one item, and I want all of them.



What am I doing wrong here? Thanks for your help guys!










share|improve this question
























  • I'm not sure what happens if you use .text on a collection of multiple elements, as you are doing here. Does it work if you just loop with for synonymesautres in synonyme.find_all("a", class_="lien3"): and inside it do print(synonymesautres.text)?
    – Robin Zigmond
    Nov 12 at 9:21










  • It's working now… What a stupid mistake! Thanks for the help!
    – BeatJuice
    Nov 12 at 9:33













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm trying to parse HTML from a website, where there are multiple elements having the same class ID. I can't seem to find a solution; I manage to get one item but not all of them.



Here's a bit of the HTML I'm trying to parse :



<h1>Synonymes travail</h1>
<div class="container-bloc1">
<strong> Nom</strong>
<br/>
-
<i><a class="lien2" href="/fr/accouchement.html"> accouchement </a></i>
:
<a class="lien3" href="/fr/gésine.html"> gésine</a>
<br/>
-
<i> <a class="lien2" href="/fr/action.html"> action </a></i>
:
<a class="lien3" href="/fr/activité.html"> activité</a>
,
<a class="lien3" href="/fr/labeur.html"> labeur</a>
</div>


In Python, I wrote it like this :



from bs4 import BeautifulSoup
import requests
import csv

source = requests.get("http://www.synonymes.net/fr/travail.html").text

soup = BeautifulSoup(source, "lxml")

for synonyme in soup.find_all("div", class_="container-bloc1"):
print(synonyme)

synonymesdumot = synonyme.find("a", class_="lien2").text
print(synonymesdumot)

for synonymesautres in synonyme.find_all("a", class_="lien3").text:
print(synonymesautres)


The first part is working, since there is only one "lien2" in the HTML file. I could do the same for "lien3" but I'd only get one item, and I want all of them.



What am I doing wrong here? Thanks for your help guys!










share|improve this question















I'm trying to parse HTML from a website, where there are multiple elements having the same class ID. I can't seem to find a solution; I manage to get one item but not all of them.



Here's a bit of the HTML I'm trying to parse :



<h1>Synonymes travail</h1>
<div class="container-bloc1">
<strong> Nom</strong>
<br/>
-
<i><a class="lien2" href="/fr/accouchement.html"> accouchement </a></i>
:
<a class="lien3" href="/fr/gésine.html"> gésine</a>
<br/>
-
<i> <a class="lien2" href="/fr/action.html"> action </a></i>
:
<a class="lien3" href="/fr/activité.html"> activité</a>
,
<a class="lien3" href="/fr/labeur.html"> labeur</a>
</div>


In Python, I wrote it like this :



from bs4 import BeautifulSoup
import requests
import csv

source = requests.get("http://www.synonymes.net/fr/travail.html").text

soup = BeautifulSoup(source, "lxml")

for synonyme in soup.find_all("div", class_="container-bloc1"):
print(synonyme)

synonymesdumot = synonyme.find("a", class_="lien2").text
print(synonymesdumot)

for synonymesautres in synonyme.find_all("a", class_="lien3").text:
print(synonymesautres)


The first part is working, since there is only one "lien2" in the HTML file. I could do the same for "lien3" but I'd only get one item, and I want all of them.



What am I doing wrong here? Thanks for your help guys!







python html parsing beautifulsoup






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 9:52









Manish Patel

3,1641721




3,1641721










asked Nov 12 at 9:02









BeatJuice

223




223












  • I'm not sure what happens if you use .text on a collection of multiple elements, as you are doing here. Does it work if you just loop with for synonymesautres in synonyme.find_all("a", class_="lien3"): and inside it do print(synonymesautres.text)?
    – Robin Zigmond
    Nov 12 at 9:21










  • It's working now… What a stupid mistake! Thanks for the help!
    – BeatJuice
    Nov 12 at 9:33


















  • I'm not sure what happens if you use .text on a collection of multiple elements, as you are doing here. Does it work if you just loop with for synonymesautres in synonyme.find_all("a", class_="lien3"): and inside it do print(synonymesautres.text)?
    – Robin Zigmond
    Nov 12 at 9:21










  • It's working now… What a stupid mistake! Thanks for the help!
    – BeatJuice
    Nov 12 at 9:33
















I'm not sure what happens if you use .text on a collection of multiple elements, as you are doing here. Does it work if you just loop with for synonymesautres in synonyme.find_all("a", class_="lien3"): and inside it do print(synonymesautres.text)?
– Robin Zigmond
Nov 12 at 9:21




I'm not sure what happens if you use .text on a collection of multiple elements, as you are doing here. Does it work if you just loop with for synonymesautres in synonyme.find_all("a", class_="lien3"): and inside it do print(synonymesautres.text)?
– Robin Zigmond
Nov 12 at 9:21












It's working now… What a stupid mistake! Thanks for the help!
– BeatJuice
Nov 12 at 9:33




It's working now… What a stupid mistake! Thanks for the help!
– BeatJuice
Nov 12 at 9:33












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










If you the code as is in your question, you run into an AttributeError because the output of .find_all() is a collection of tags (a ResultSet more specifically) that has no attribute text; but each of its elements, which are of type bs4.Element.Tag, do. So you need to get the text attribute for each of the tags inside the for loop:



for synonymesautres in synonyme.find_all("a", class_="lien3"):
print(synonymesautres.text)


Output:



le
travail
manque
de
travail
travail
fatigant





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258797%2fparsing-mutiple-items-using-beautifulsoup-in-python%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    If you the code as is in your question, you run into an AttributeError because the output of .find_all() is a collection of tags (a ResultSet more specifically) that has no attribute text; but each of its elements, which are of type bs4.Element.Tag, do. So you need to get the text attribute for each of the tags inside the for loop:



    for synonymesautres in synonyme.find_all("a", class_="lien3"):
    print(synonymesautres.text)


    Output:



    le
    travail
    manque
    de
    travail
    travail
    fatigant





    share|improve this answer

























      up vote
      0
      down vote



      accepted










      If you the code as is in your question, you run into an AttributeError because the output of .find_all() is a collection of tags (a ResultSet more specifically) that has no attribute text; but each of its elements, which are of type bs4.Element.Tag, do. So you need to get the text attribute for each of the tags inside the for loop:



      for synonymesautres in synonyme.find_all("a", class_="lien3"):
      print(synonymesautres.text)


      Output:



      le
      travail
      manque
      de
      travail
      travail
      fatigant





      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        If you the code as is in your question, you run into an AttributeError because the output of .find_all() is a collection of tags (a ResultSet more specifically) that has no attribute text; but each of its elements, which are of type bs4.Element.Tag, do. So you need to get the text attribute for each of the tags inside the for loop:



        for synonymesautres in synonyme.find_all("a", class_="lien3"):
        print(synonymesautres.text)


        Output:



        le
        travail
        manque
        de
        travail
        travail
        fatigant





        share|improve this answer












        If you the code as is in your question, you run into an AttributeError because the output of .find_all() is a collection of tags (a ResultSet more specifically) that has no attribute text; but each of its elements, which are of type bs4.Element.Tag, do. So you need to get the text attribute for each of the tags inside the for loop:



        for synonymesautres in synonyme.find_all("a", class_="lien3"):
        print(synonymesautres.text)


        Output:



        le
        travail
        manque
        de
        travail
        travail
        fatigant






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 9:33









        Edgar R. Mondragón

        1,4061619




        1,4061619






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258797%2fparsing-mutiple-items-using-beautifulsoup-in-python%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine