繁体   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)

如您所见,我的打印语句在 if 循环之后不起作用。 那是:

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

我在这里搞砸了什么? 在 if 循环之后打破整个 function 的主要原因是什么? 请帮助我,并提前谢谢!

如果您的意思是在 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--------#

如果您要在 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--------#

这里的问题是两个错误:

  • 实际问题是,在某些时候booking.text失败,因为 bs4 没有找到带有class=bookingdiv ,所以它返回None ,它没有属性.text ,所以需要一个 excpeion 。 只需检查 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)))

(最好也为rest_name这样做)

  • 您没有看到错误消息的原因是因为它隐藏在您在循环中执行的打印墙后面。 这些是缓冲的,并且可能在稍后发生,例如在应用程序退出时。 但是,在应用程序退出时,错误消息已经被打印(未缓冲),然后打印将它们隐藏起来。 始终检查错误。

暂无
暂无

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

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