简体   繁体   English

在 if 循环之后不打印语句

[英]Not printing the statement after the if loop

from bs4 import BeautifulSoup
import numpy as np
import requests 
from selenium import webdriver
from nltk.tokenize import sent_tokenize,word_tokenize
print('ansife')
# html = requests.get('https://www.opentable.com/new-york-restaurant-listings')
# driver = webdriver.Firefox(,executable_path=r'[Your path]\geckodriver.exe')
html = webdriver.Firefox(executable_path=r'D:\geckodriver.exe')
html.get("https://www.opentable.com/new-york-restaurant-listings")
counter = 0
lists = []
def parser_NYU(html):

    global counter,lists
    total_hotels_more_than_60_bookings = 0
    soup = BeautifulSoup(html.page_source,'lxml')
    for i, restroom in enumerate(soup.find_all('div',class_='rest-row-info')):
        rest_name = restroom.find('span',class_='rest-row-name-text').text
        booking = restroom.find('div',class_='booking')  #.text
        words = list(word_tokenize(str(booking.text)))
        #same day

        if int(words[1]) > 100:
            print(booking.text)
            lists.extend([booking.text])

        print('listers',len(lists))
        print('this works fine')
        print('this works fine')    

    print('listers',len(lists))
    print('unfortunately this not works,why?')
    print('unfortunately this not works,why?')      

        
parser_NYU(html)

As you can see my print statements are not working after the if loop.如您所见,我的打印语句在 if 循环之后不起作用。 that is:那是:

    print('listers',len(lists))
    print('this is not printing')  # not printing 
    print('this is not printing')

what did i messed up here?我在这里搞砸了什么? what will be the main reason behind breaking the entire function after that if loop?在 if 循环之后打破整个 function 的主要原因是什么? please help me, and advance thanks!请帮助我,并提前谢谢!

If you mean to print the statements inside the if-condition (not if-loop) add the statements with proper indentation ie under the scope of if-condition.如果您的意思是在 if 条件(不是 if 循环)中打印语句,请添加带有适当缩进的语句,即在 if 条件的 scope 下。

    for i, restroom in enumerate(soup.find_all('div',class_='rest-row-info')):
        rest_name = restroom.find('span',class_='rest-row-name-text').text
        booking = restroom.find('div',class_='booking')  #.text
        words = list(word_tokenize(str(booking.text)))
        #same day

        if int(words[1]) > 100:
#-----------Scope of if-block starts here--------#
            print(booking.text)
            lists.extend([booking.text])

            print('listers',len(lists))
            print('this is not printing')  # not printing 
            print('this is not printing')
#-----------Scope of if-block ends here--------#

If you mean to print it inside the for-loop and not if-condition, place the print statements under the scope of for-loop如果您要在 for 循环而不是 if 条件中打印它,请将打印语句放在 for-loop 的 scope 下

    for i, restroom in enumerate(soup.find_all('div',class_='rest-row-info')):
#-------Scope of for-block starts here--------#
        rest_name = restroom.find('span',class_='rest-row-name-text').text
        booking = restroom.find('div',class_='booking')  #.text
        words = list(word_tokenize(str(booking.text)))
        #same day

        if int(words[1]) > 100:

            print(booking.text)
            lists.extend([booking.text])

        print('listers',len(lists))
        print('this is not printing')  # not printing 
        print('this is not printing')
#-------Scope of for-block endshere--------#

The problem here is two fault:这里的问题是两个错误:

  • The actual issues is that at some point booking.text fails because bs4 does not find a div with class=booking , so it returns None , which does not have the attribute .text , so an excpeion is thorwn.实际问题是,在某些时候booking.text失败,因为 bs4 没有找到带有class=bookingdiv ,所以它返回None ,它没有属性.text ,所以需要一个 excpeion 。 Just check if find returned None :只需检查 find 是否返回None
        rest_name = restroom.find('span',class_='rest-row-name-text').text
        booking = restroom.find('div',class_='booking')
        if booking is None:
            continue
        words = list(word_tokenize(str(booking.text)))

(best also do that for rest_name ) (最好也为rest_name这样做)

  • The reason you are not seeing the error message is because it is hidden behind the wall of prints you are doing in the loop.您没有看到错误消息的原因是因为它隐藏在您在循环中执行的打印墙后面。 Those are buffered, and might happen at a latter point, eg at application exit.这些是缓冲的,并且可能在稍后发生,例如在应用程序退出时。 However at application exit, the error message has already been printed (unbufferd) and the prints afterward hide them.但是,在应用程序退出时,错误消息已经被打印(未缓冲),然后打印将它们隐藏起来。 Always check for errors.始终检查错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM