[英]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:这里的问题是两个错误:
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=booking
的div
,所以它返回None
,它没有属性.text
,所以需要一个 excpeion 。 Just check if find returned None
: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
这样做)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.