Python Forum
For Loop Returning 3 Results When There Should Be 1
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For Loop Returning 3 Results When There Should Be 1
#8
Hi snippsat,

Thank you for your advice about not using soup when looping- I had tried over 30 different methods to get this data and most of them didn't use soup when looping, but by the end of all those failures- I then tried soup Shocked and off course that didn't work either! But good to know never to use it for looping.

Also, thank you for teaching me the easier way to call a class. That's soooo much easier than what I've always done. I have seen your method before, but as I'm still learning, I didn't want to try and learn too many variations and confuse myself more. Big Grin

With regards to parser, I've only ever used one: html.parser.

So I followed your suggestion to use lxml and tried the following code:

from bs4 import BeautifulSoup

with open(r"out_of_stock2.html", encoding="utf8") as fp:
    soup = BeautifulSoup(fp, 'lxml')
    ph1 = soup.find_all('div', class_ ='h-100 pb1-xl pr4-xl pv1 ph1')
    for item in ph1:
        mt1_ph1 = item.find('span', class_ = 'w_A w_C w_B mr1 mt1 ph1')
        if mt1_ph1 is None:
            print('No data')
        else:
            print(mt1_ph1.text)
The result it returned:
Output:
No data 1-day shipping
Dance You fixed it! Thank you so much. I've Wall for 2 days trying to figure it out- and honestly probably wouldn't have thought of trying your option. Really appreciate it.



(Sep-26-2021, 06:36 AM)snippsat Wrote: Should not loop over soup object knight2000,as it's not needed and can give unwanted result.
It will depend on parser used,so if i use lxml(recommend) as parser the length will be one.
from bs4 import BeautifulSoup

with open(r"out_of_stock2.html", encoding="utf8") as fp:
    soup = BeautifulSoup(fp, 'lxml')
    print(len(soup))
    mt2 = soup.find('span', class_="w_A w_C w_B mr1 mt1 ph1")
    if mt2 is None:
        print('There is no record')
    else:
        print (mt2)
Output:
1 <span class="w_A w_C w_B mr1 mt1 ph1">1-day shipping</span>
It's easier to use class_="w_A w_C w_B mr1 mt1 ph1 than make it a dictionary call.
Then can just copy CSS class from web-site and add one _.
Reply


Messages In This Thread
RE: For Loop Returning 3 Results When There Should Be 1 - by knight2000 - Sep-26-2021, 11:05 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,053 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  Help add for loop results in a list paulo79 4 1,542 Mar-09-2022, 05:49 PM
Last Post: deanhystad
  returning values in for loop Nickd12 4 12,166 Dec-17-2020, 03:51 AM
Last Post: snippsat
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,233 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  Adding loop results as rows in dataframe Shreya10o 2 2,188 May-09-2020, 11:00 AM
Last Post: Shreya10o
  How to append one function1 results to function2 results SriRajesh 5 3,145 Jan-02-2020, 12:11 PM
Last Post: Killertjuh
  Returning true or false in a for loop bbop1232012 3 8,129 Nov-22-2018, 04:44 PM
Last Post: bbop1232012
  RegExp: returning 2nd loop in new document syoung 5 3,865 May-02-2018, 12:36 PM
Last Post: syoung

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020