Home > Software design >  Count number of 0 in a list of list web scraped
Count number of 0 in a list of list web scraped

Time:09-18

i want calculate the number of time a 0 appears in a "list of lists" web scraped, it's like my list of lists is not a list because when i print, it's done that :

[3]
[1]
[3]
[3]
[1]
[1]
[0]
[0]
[1]
[1]

if it's really is a list of list it's should tell me this :

[
[3],
[1],
[3],
[3],
[1],
[1],
[0],
[0],
[1],
[1]
]

so, list or not list i cant count the number of time the 0 is here, how can i do for count the 0 ?

in this exemple, there is two 0, so the result is = 2

There is my code, maybe you can tell me how i can, i tried count(0) and other method, but it's dont work

from bs4 import BeautifulSoup
import requests
from csv import writer

url= "https://blablablabla.htm"
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')

lists = soup.find_all('tr', class_="mat1")

with open('file.csv', 'w', encoding='utf8', newline='') as f:
    thewriter = writer(f)

for list in lists: 
        
        but = list.find('td', class_="tj1").text.replace('\xa0', '').replace(" ", "1")
        
        info = but[3:]

        infos = len(info)
        infoss = int(infos)
        print(infoss)

print(infoss) is :

3
1
3
3
1
1
0
0
1
1

and i need a code which tell me the number of 0 in this list, so here it's 2

thanks a lot guys, i tried my best

CodePudding user response:

You are already iterating through the list of lists and printing out the values within it.

Can you think of a way to check the value instead of printing it and keep track of the number of times the value is equal to 0?

EDIT: Are you saying that this code doesn't work?

import requests
from csv import writer

url= "https://blablablabla.htm"
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')

lists = soup.find_all('tr', class_="mat1")

with open('file.csv', 'w', encoding='utf8', newline='') as f:
    thewriter = writer(f)

coun = 0
for list in lists: 
        
        but = list.find('td', class_="tj1").text.replace('\xa0', '').replace(" ", "1")
        
        info = but[3:]

        infos = len(info)
        infoss = int(infos)
        print(infoss)
        
        if infoss == 0 :
            coun = coun   1
        

print(coun)

CodePudding user response:

i tried this :

from bs4 import BeautifulSoup
import requests
from csv import writer

url= "https://blablablabla.htm"
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')

lists = soup.find_all('tr', class_="mat1")

with open('file.csv', 'w', encoding='utf8', newline='') as f:
    thewriter = writer(f)

for list in lists: 
        
        but = list.find('td', class_="tj1").text.replace('\xa0', '').replace(" ", "1")
        
        info = but[3:]

        infos = len(info)
        infoss = int(infos)
        print(infoss)
        
        coun = 0
        if infoss < 1 :
            coun = coun   1
        
        print(coun)

the result is this :

0
0
0
0
0
0
1
1
0
0

yes it's tell me 1 and 1, but i dont want that form of answer, i just want it's tell me result = 2, without all the rest

i tried to just have the number of item with a len, but it's seems to me, it's a bunch of list, but they are not in big list as they should to be. I cant track nothing, they seems not linked betwwen them in a big list.

I tried a dico method, and doesnt work too

CodePudding user response:

You could try creating new list to store value of infoss then count element infoss that has 0.

lst_infoss = []
for list in lists: 
    
    but = list.find('td', class_="tj1").text.replace('\xa0', '').replace(" ", "1")
    
    info = but[3:]

    infos = len(info)
    infoss = int(infos)
    print(infoss)
    # append infoss to new list (lst_infoss)
    lst_infoss.append(infoss)

print(lst_infoss)    
zero_infoss = lst_infoss.count(0)
print(zero_infoss)
  • Related