在网页中搜索字符串并使用 python 打印包含它的整行 [英] Search for a string in webpage and print the entire line containing it using python

查看:56
本文介绍了在网页中搜索字符串并使用 python 打印包含它的整行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在网页上搜索一个字符串并打印包含该字符串的整行.

I would like to search a webpage for a string and print the entire line containing that string.

我有一个输入文件,其中包含我想搜索该字符串的链接.

I have a input file containing the links that i would like to search for that string.

要搜索的字符串:vcore"

我的输入文件:

http://abc/cluster/app/application_1447334090028_225490
http://abc/cluster/app/application_1447334090028_228858

预期输出文件:

http://abc/cluster/app/application_1447334090028_225490    12434 vcore, 123 mb
http://abc/cluster/app/application_1447334090028_228858    12132 vcore, 131 mb

目前的代码:

import sys
import re
import urllib

Links = [Link.strip() for Link in open ('/home/try/Input.txt','r').readlines()]

for link in Links:
        webPage = urllib.urlopen(link).read()
        print webPage

然后我使用 grep 搜索字符串并将其存储在另一个文件中.但我希望它由代码本身完成,并且该行出现在相应链接旁边.有人可以帮我吗?

Then i use grep to search for the string and store it in another file. But i want it to be done by the code itself and the line to appear next to the corresponding link. Can anyone help me on this?

推荐答案

lines = urllib.urlopen(link).readlines()
for line in lines:
    if "vcore" in line:
        print line

这篇关于在网页中搜索字符串并使用 python 打印包含它的整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆