首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >API而不是BeautifulSoup?

API而不是BeautifulSoup?
EN

Stack Overflow用户
提问于 2021-02-11 16:16:04
回答 1查看 32关注 0票数 0

本文档定义了一些MS服务的URL和IP:https://docs.microsoft.com/en-us/microsoft-365/enterprise/urls-and-ip-address-ranges?view=o365-worldwide#exchange-online

我的目标是编写一个Python脚本来检查这个文档的最后更新日期。如果日期发生了变化(意味着某些IP发生了变化),我需要立即知道。我找不到任何API来实现这个目标,所以我写了这个脚本:

代码语言:javascript
复制
from bs4 import BeautifulSoup
import requests
import time
import re

url = "https://docs.microsoft.com/en-us/microsoft-365/enterprise/urls-and-ip-address-ranges?view=o365-worldwide#exchange-online"

#set the headers as a browser
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}

while True:
    response = requests.get(url,headers=headers)
    soup = BeautifulSoup(response.text,"html.parser")
    last_update_fra = soup.find(string=re.compile("01/04/2021"))
    time.sleep(60)
    soup = BeautifulSoup(requests.get(url, headers=headers).text, "html.parser")
    if soup.find(string=re.compile("01/04/2021")) == last_update_fra:
        print(last_update_fra)
        continue
    else:
        #send an email for notification
        pass

我不确定这是不是最好的方法。因为如果日期将更改,我还需要将我的脚本更新为另一个日期(更新日期)。另外,这可以用BeautifulSoup来做吗?或者有另一种更好的方法?

EN

回答 1

Stack Overflow用户

发布于 2021-02-11 16:57:57

这里的汤很好。我甚至看不到有数据的XHR请求。

我注意到了几件事:

  1. 您真的想/需要每分钟检查一次吗?也许更好的每天/24小时,或者曾经12或6小时?
  2. 如果在任何时候崩溃,即你失去了互联网连接或得到400响应,你将需要重新启动脚本并丢失任何最后的日期。因此,可以考虑a)将日期写到某个磁盘上,这样它就不只是存储在内存中了,b)在其中放入一些try/exceptions,这样如果它遇到错误,它就会继续运行,并在下一个时间间隔重试(或者无论您如何决定重试)。

代码:

代码语言:javascript
复制
import requests
import time
from bs4 import BeautifulSoup

url = 'https://docs.microsoft.com/en-us/microsoft-365/enterprise/urls-and-ip-address-ranges?view=o365-worldwide#exchange-online'

#set the headers as a browser
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}

last_update_fra = ''
while True:
    time.sleep(60)
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')
    found_date = soup.find('time').text
    if found_date == last_update_fra:    
        print(last_update_fra)
        continue
    else:
        # store new date
        last_update_fra = found_date
        
        #send an email for notification
        pass
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66151005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档