当if语句不存在时,我的代码运行良好。当选择的字符串出现在网站上时,我只想打开和关闭伺服电动机(一定要点一下)。好吧,当代码在if语句之外时,它可以正常运行;当它在语句中时,它可以运行,但它与外面的代码不同,它只是将伺服打开,而不是将其设置为关闭(就像它在语句之外一样)。这正常吗?我不知道问题可能在哪里。
import RPi.GPIO as GPIO
import time
from bs4 import BeautifulSoup
import requests
channel = 17
# GPIO setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.OUT)
def motor_on(pin):
GPIO.output(pin, GPIO.HIGH) # Turn motor on
def motor_off(pin):
GPIO.output(pin, GPIO.LOW) # Turn motor off
url = 'https://www.psc.cz/36301/'
data = requests.get(url).content
soup = BeautifulSoup(data, 'html.parser')
#searched = soup.find_all('td', class_="TdAltNazev")
searched = soup.find_all('h1')
if 'Ostrov' in searched:
motor_on(channel)
time.sleep(5)
motor_off(channel)
time.sleep(5)
GPIO.cleanup()