簡體   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