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
#1
Hi Guys,

After trying to figure this one out for over 8 hours, I thought I would get a fresh perspective from someone.

I'm practicing some web scrapping and I've got a scenario where I've got a pretty easy goal: I'm trying to find an object and if it exists, extract some data from it (shipping information) and if it doesn't exist, enter something like " " (...because I'm going to be using pandas- so I need to do something when it can't find the object, else I know I'll get the "ValueError Arrays Must be All Same Length" error).

I've tried many things to do this, but I'm unable to successfully:
1) capture where the object doesn't exist; and
2) accurately get data from when the object does exist.

My current reiteration of the code is:

from bs4 import BeautifulSoup

with open("out_of_stock2.html", encoding="utf8") as fp:
    soup = BeautifulSoup(fp, 'html.parser')
    for item in 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)
When I run this, I get:
Output:
<span class="w_A w_C w_B mr1 mt1 ph1">1-day shipping</span> <span class="w_A w_C w_B mr1 mt1 ph1">1-day shipping</span> <span class="w_A w_C w_B mr1 mt1 ph1">1-day shipping</span>
I'm not sure why I'm getting 3 instances of this when the data only contains 1? (The object I'm looking for is "w_A w_C w_B mr1 mt1 ph1")

Additionally, there is one record in the dataset that doesn't contain the object but the code output ignores my print statement ('There is no record')

Could someone please shed some light on what I'm doing incorrectly?

Thank you.

Attached Files

.html   out_of_stock2.html (Size: 7.61 KB / Downloads: 251)
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,072 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  Help add for loop results in a list paulo79 4 1,567 Mar-09-2022, 05:49 PM
Last Post: deanhystad
  returning values in for loop Nickd12 4 12,272 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,243 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  Adding loop results as rows in dataframe Shreya10o 2 2,197 May-09-2020, 11:00 AM
Last Post: Shreya10o
  How to append one function1 results to function2 results SriRajesh 5 3,175 Jan-02-2020, 12:11 PM
Last Post: Killertjuh
  Returning true or false in a for loop bbop1232012 3 8,157 Nov-22-2018, 04:44 PM
Last Post: bbop1232012
  RegExp: returning 2nd loop in new document syoung 5 3,886 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