[英]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=booking
的div
,所以它返回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.