【问题标题】:How to extract content with search words in html page using Python如何使用Python在html页面中提取带有搜索词的内容
【发布时间】:2020-09-28 11:10:19
【问题描述】:

在html页面中,有多个

,想打印匹配单词的上下文
<pre>
currently missing servers:
 vm1 vm2 vm3
currently shutdown servers:
 vm4 vm5 vm6
</pre>
<pre>
Prod server:
 prdvm1 prodvm2
</pre>

从上面的输出,只想打印

currently missing servers:
  vm1 vm2 vm3

【问题讨论】:

  • 请提供您从中提取此数据的 URL。
  • 这是内部服务器,网址不可公开访问。我必须将该网页中的 UTC 与实时 UTC 值进行比较。我已经粘贴了 URL 显示的 .html 内容

标签: python html


【解决方案1】:

此答案根据您给出的 html 为您提供了您所要求的内容。但如果 html 发生更改,它可能无法为您提供正确的结果。

import requests
from bs4 import BeautifulSoup
URL = ""            #Give your URL here
r = requests.get(URL)
soup = BeautifulSoup(r.content, 'html5lib')
blockquote = soup.find_all('blockquote')[0]
strong = blockquote.find_all('strong')
for element in strong:
    element.extract()
blockquote = blockquote.text.split(', ')[1].split(' ')[1]
print(blockquote)

【讨论】:

  • 它有帮助,我在我的设置中将 html5lib 更改为 hrml.parser。我需要将实时 UTC 与从上述脚本中提取的 UTC 进行比较。如果 .html UTC 时间小于实时 UTC 5 分钟,我需要创建警报
  • from datetime import datetime import requests from bs4 import BeautifulSoup URL = "MY_URL" r = requests.get(URL) soup = BeautifulSoup(r.content, 'html.parser') blockquote = soup.find_all('blockquote')[0] strong = blockquote.find_all('strong') for element in strong: element.extract() jprt_time = blockquote.text.split(', ')[1].split(' ')[1] now = datetime.now() utc_now = datetime.utcnow() utc = utc_now.strftime("%H:%M:%S") print(utc) print(jprt_time) if jprt_time == utc: print ("PROD is running fine") else: print("PROD is down")
  • 请格式化您的代码。以便对其他人有用。
  • Stackoverflow 评论部分不允许格式化,否则我已将我的问题添加为“回答您的问题”
  • 简而言之,我正在尝试使用“timedelta”将提取的输出与实时进行比较,如果差异大于 5 分钟,则需要发送警报“如果 jprt_time
【解决方案2】:

试试这个:

import re

from bs4 import BeautifulSoup

html = """
  <meta http-equiv="Pragma" content="no-cache">
  <meta http-equiv="Expires" content="-1">
  <meta http-equiv="Cache-Control" content="no-cache">
  <meta http-equiv="refresh" content="60" >
  <html> 
      <title>PROD> VM</title>
      <h1>Production VM</h1>
      <blockquote> 
          <strong>As of:</strong>Monday September 7, 2020 12:12:35 UTC, Mon-12:12:35-GMT <br>
          <strong>Wiki: <a href="http:proddashboard.com"</a></strong>
   </blockquote>
"""

soup = BeautifulSoup(html, "html.parser").find("blockquote").getText()
_time = re.findall(r'\d+:\d+:\d+\sUTC', soup)
print(_time)

输出:

['12:12:35 UTC']

编辑:

基于 OP 的 cmets,这是一种更新的方法:

from datetime import datetime, timedelta
import re

from bs4 import BeautifulSoup

html = """
  <meta http-equiv="Pragma" content="no-cache">
  <meta http-equiv="Expires" content="-1">
  <meta http-equiv="Cache-Control" content="no-cache">
  <meta http-equiv="refresh" content="60" >
  <html> 
      <title>PROD> VM</title>
      <h1>Production VM</h1>
      <blockquote> 
          <strong>As of:</strong>Monday September 7, 2020 12:12:35 UTC, Mon-12:12:35-GMT <br>
          <strong>Wiki: <a href="http:proddashboard.com"</a></strong>
   </blockquote>
"""
soup = BeautifulSoup(html, "html.parser").find("blockquote")
result = ''.join(re.findall(r'As of:(.*)\sUTC', soup.getText()))
date = datetime.strptime(result, "%A %B %d, %Y %H:%M:%S")

if date < datetime.utcnow() - timedelta(minutes=5):
    print("PROD is running")
else:
    print("PROD is not running")

输出:PROD is running

【讨论】:

  • 谢谢,如果有相同的标签示例,使用 Beatifulsoup
    abc
    def
    如何识别内容和打印。
  • 使用示例 HTML 和所需输出编辑您的问题,我会看看。
  • 另外,如果您觉得答案有用,请考虑投票和/或接受它。
  •  当前缺少服务器:vm1 vm2 vm3 当前关闭服务器:vm4 vm5 vm6 
     Prod 服务器:prdvm1 prodvm2 
    我只想打印“Currently Missing clients vm1 vm2 vm3"
  • 我正在尝试格式化 HTML,但在“添加评论”中没有这样做的选项
猜你喜欢
  • 2014-08-26
  • 1970-01-01
  • 1970-01-01
  • 2018-11-15
  • 2023-03-15
  • 2020-01-28
  • 2012-02-11
  • 2012-07-10
  • 1970-01-01
相关资源
最近更新 更多