简体   繁体   English

如何使用“get_detail_data”的output运行批处理文件

[英]How to use the output of “get_detail_data” to run batch file

I'm very much a beginner to python, and I've been trying to figure this out for a few days.我是 python 的初学者,几天来我一直在努力解决这个问题。 Hoping you guys can help me.希望你们能帮助我。

I'm looking to use the result of 'avail' in the below code determine whether to launch a batch file or not.我希望在下面的代码中使用“avail”的结果来确定是否启动批处理文件。 For example, if the result is "YES" then run this batch file and end script.例如,如果结果为“YES”,则运行此批处理文件并结束脚本。 If the result is "NO" loop back and run again until "YES" is found.如果结果为“NO”,则返回并再次运行,直到找到“YES”。

import  requests
from bs4 import BeautifulSoup

def get_page(url):
    response = requests.get(url)

    if not response.ok:
        print('Server responded:', response.status_code)
    else:
        soup = BeautifulSoup(response.text, 'lxml')
    return soup

def get_detail_data(soup):

    avail = soup.find('span', id='availability').text.strip()
    print(avail)

def main():
    url ='https://www.blahblah.com'

    get_detail_data(get_page(url))

if __name__ == '__main__':
    main()

To answer the question:要回答这个问题:

# Here is one way we could implement get_detail_data (its a bit verbose)
def get_detail_data(soup):
    avail = soup.find('span', id='availability').text.strip()
    if avail == "YES":
        return True
    elif avail == "NO":
        return False
    else: 
        print("Unexpected value")
        return None

# Or a slightly cleaner way (returns True if and only if avail == "YES"
def get_detail_data(soup):
    avail = soup.find('span', id='availability').text.strip()
    return avail == "YES"

Next we would write the function that handles running the batch file.接下来我们将编写处理运行批处理文件的 function。

def run_batch_file():
    # I will leave this up to you to implement

Now your main function could look something like this:现在您的主要 function 可能看起来像这样:

def main():
    url ='https://www.blahblah.com'

    # This is how we create an infinite loop in python
    while True:
        # check to see if its available
        is_available = get_detail_data(get_page(url))
        if is_available:
            # if its available, we run the batch file, then break out of the loop
            # (It might help to read up on loops, and the break keyword in python)
            run_batch_file()
            break
        
        # if the program gets to this line, then is_available was false
        #   so we will wait for a little bit before checking again
        #   (it might help to read up on time.sleep)
        time.sleep(10)    

Since you are a python beginner, I have a comment regarding your code:由于您是 python 初学者,我对您的代码有一个评论:

  • In the get_page function, you only define soup in the else clause.get_page function 中,您只需在 else 子句中定义soup If the if condition is true, soup will not be set, therefore return soup will throw an error.如果if条件为真,则不会设置汤,因此return soup会抛出错误。

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

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