关于芬德尔在一行teks上打印全文

2024-10-01 17:32:46 发布

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

我得到了以下代码:

import urllib
import re

html = urllib.urlopen("http://jshawl.com/python-playground/").read()

lines = [html]
for line in lines:
    if re.findall("jesseshawl", line):
        print line

当我运行这段代码时,我的输出是它将返回完整的网站。我怎么能只显示找到“jesseshawl”的行。它应该返回如下内容:

^{pr2}$

有没有一种方法可以在我运行这个时不返回所有的html标记?在

我的输出:

<html>
<head></head>
<body>
<h1>Some images to download:</h1>
<img src='python.gif'/><br />
<img src='terminal.png' />
<hr />

<h1>Email addresses to extract:</h1>
jesseshawl@gmail.com<br />
sudojesse@gmail.com<br />

<hr />

<h1>Login Form:</h1>
Login here:<br />
User: user<br />
Pass: pass
<form method="POST" action="login.php">
 User: <input type="text" name="username" /><br />
 Pass: <input type="password" name="password" /><br />
 <input type="submit" />
</form>

<h1>Memorable Quotes</h1>
<ul>
    <li></li>
</ul>

</body>
</html>

Tags: 代码brimportrecominputhtmltype
1条回答
网友
1楼 · 发布于 2024-10-01 17:32:46

你在读整页。它打印所有的东西。你必须逐行阅读排队。那里不需要findall可以使用in运算符

代码:

import urllib
import re

html = urllib.urlopen("http://jshawl.com/python-playground/").readlines()
for line in html :
    if "jesseshawl" in line:
        print line

输出:

^{pr2}$

如果不需要标记,可以使用sub删除它们

代码2:

import urllib
import re

html = urllib.urlopen("http://jshawl.com/python-playground/").readlines()
for line in html :
    if "jesseshawl" in line:
        print re.sub("<[^>]*?>","",line)

输出2:

jesseshawl@gmail.com

相关问题 更多 >

    热门问题