给定http lin的源代码中的Grep字符串

2024-09-20 22:21:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用bash命令行在给定网页的源代码中查找字符串。我会手动执行以下步骤:

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

有没有一种方法可以使用bash命令或者python的其他方法来实现这一点?你知道吗


Tags: 方法字符串命令行https命令combash网页
3条回答

您可以使用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

这符合你的要求吗?你知道吗

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

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

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

相关问题 更多 >

    热门问题