【问题标题】:Grep string in the source code of a given http link给定 http 链接的源代码中的 Grep 字符串
【发布时间】:2019-07-08 22:58:37
【问题描述】:

我想使用 bash 命令行在给定网页的源代码中查找字符串。手动我会按照这些步骤进行操作:

  1. 打开https://stackoverflow.com
  2. cltrl+U(在谷歌浏览器中获取源代码)
  3. cltrl+F 查找字符串

有没有办法使用 bash 命令或使用 python 的任何其他方式来做到这一点?

【问题讨论】:

  • 美汤是一个python模块,可能有你需要的东西
  • 请注意,html 上的 grep 可能无法为您提供您希望的一致结果,具体取决于您的字符串及其特定来源。如果可能,了解您的数据。
  • @MitchelPaulin 你能推荐我任何你知道的模块吗?
  • @PaulHodges 哦,好吧..还有其他选择吗?
  • 美汤...?

标签: python bash shell command-line


【解决方案1】:

假设您可以访问curlgrep,那么只需:

bash$ curl http://some-website.com | grep -F some-string

【讨论】:

    【解决方案2】:

    您可以使用 curl 或 wget CLI 工具来获取页面,然后只需 grep 结果(或管道到 grep)。

    $ curl https://stackoverflow.com/questions/54698492/grep-string-in-the-source-code-of-a-given-http-link | grep "string in the source code"
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
      0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0        <title>python - Grep string in the source code of a given http link - Stack Overflow</title>
            <meta name="twitter:title" property="og:title" itemprop="name" content="Grep string in the source code of a given http link" />
            <meta name="twitter:description" property="og:description" itemprop="description" content="I want to use bash commands line to find a string in the source code of a given web page. Manually I would do it like in those steps:
                <link rel="alternate" type="application/atom+xml" title="Feed for question &#39;Grep string in the source code of a given http link&#39;" href="/feeds/question/54698492">
                        <h1 itemprop="name" class="grid--cell fs-headline1 fl1 ow-break-word"><a href="/questions/54698492/grep-string-in-the-source-code-of-a-given-http-link" class="question-hyperlink">Grep string in the source code of a given http link</a></h1>
    <p>I want to use bash commands line to find a string in the source code of a given web page. Manually I would do it like in those steps:</p>
    100  102k  100  102k    0     0   160k      0 --:--:-- --:--:-- --:--:--  159k
    

    如果您只想要字符串,请使用 grep -o:

    $ curl https://stackoverflow.com/questions/54698492/grep-string-in-the-source-code-of-a-given-http-link 2>/dev/null | grep -o "string in the source code"
    string in the source code
    string in the source code
    string in the source code
    string in the source code
    string in the source code
    string in the source code
    string in the source code
    string in the source code
    string in the source code
    string in the source code
    string in the source code
    string in the source code
    string in the source code
    

    【讨论】:

      【解决方案3】:

      这是否符合您的要求?

      curl -s https://stackoverflow.com | grep "string"
      

      【讨论】:

        【解决方案4】:

        有一次我需要从 nagios Web 仪表板中解析一些值,

        • 用过wget
        • 将内容保存在文件中(-O 文件名)
        • 使用grep 从文件中获取所需的值

          wget --user='username' --password='password' https://nagios/dashboard -O filename

        【讨论】:

          猜你喜欢
          • 2013-03-23
          • 1970-01-01
          • 1970-01-01
          • 2017-05-26
          • 2019-05-03
          • 1970-01-01
          • 2020-08-04
          • 2019-07-14
          相关资源
          最近更新 更多