【问题标题】:Python using re.compile html to set website update alertPython 使用 re.compile html 设置网站更新提醒
【发布时间】:2021-04-22 08:23:15
【问题描述】:

如果网站有任何更新,我想设置提醒。 似乎我需要使用 re.compile 来设置正则表达式,但我不熟悉,我使用:

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():

任何帮助将不胜感激!谢谢!

【问题讨论】:

  • 其实不需要hashlib或者re,直接对比整个网站即可:old.content == current.content
  • @Programmer 您好,感谢您的帮助,但如果我删除pattern,它会返回连接错误
  • 对不起,在我的机器上工作正常,看我的例子
  • 下次直接发布错误信息,请... 这个错误说明 1)xueqiu.com 的服务器正忙 2)您的互联网连接速度太慢 3)其他问题您的互联网连接错误。您可以通过浏览器访问网站吗?

标签: python html python-re


【解决方案1】:

您不需要正则表达式来执行此操作,或者,正如@DavidMeu 所暗示的,hashlib。只要检查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)

【讨论】:

  • 嗨,谢谢,但它一直在我这边返回 [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 错误,不知道为什么
猜你喜欢
  • 2016-06-05
  • 1970-01-01
  • 1970-01-01
  • 2015-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多