简体   繁体   中英

Python using re.compile html to set website update alert

I would like to set an alert if the website has any update. Seems I need to use re.compile to set regular expression, but I am not familiar with that, I use:

def Request():
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36'}     
    req = requests.get('https://xueqiu.com/u/4357540281', headers=headers)   
    content = req.content    
    pattern = re.compile('.html').findall(content) #how
    return pattern 

def update():
    print('In progress')
    old_pattern = Request() 
    while True:
        new_pattern = Request() 
        if (new_pattern!= old_pattern):  
            old_pattern=new_pattern    
            send_email()  
        else:
            now=datetime.datetime.now()
            print(now,"No Update")
        time.sleep(300)
            
            
def send_email():

Any help will be highly appreciated! thanks!

You don't need regex to do this, or, as @DavidMeu siggested, hashlib. Just check if old_request.content.= new_req.content :

def get_content():
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36'}     
    req = requests.get('https://xueqiu.com/u/4357540281', headers=headers)   
    return req.content

def update():
    print('In progress')
    old_content = get_content() 
    while True:
        new_content = Request() 
        if old_content != new_content):  
            old_content = new_content   
            send_email()  
        else:
            now = datetime.datetime.now()
            print(now, "No Update")
        time.sleep(300)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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