小编adi*_*pta的帖子

在每行匹配后使用grep获取下一个WORD

我想从我的服务器日志中获取" GET "查询.

例如,这是服务器日志

1.0.0.127.in-addr.arpa - - [10/Jun/2012 15:32:27] code 404, message File not fo$
1.0.0.127.in-addr.arpa - - [10/Jun/2012 15:32:27] "GET /hello HTTP/1.1" 404 -   
1.0.0.127.in-addr.arpa - - [10/Jun/2012 15:41:57] code 404, message File not fo$
1.0.0.127.in-addr.arpa - - [10/Jun/2012 15:41:57] "GET /ss HTTP/1.1" 404 -
Run Code Online (Sandbox Code Playgroud)

当我尝试使用简单的grep或awk时,

Adi:~ adi$ awk '/GET/, /HTTP/' serverlogs.txt
Run Code Online (Sandbox Code Playgroud)

它给出了

1.0.0.127.in-addr.arpa - - [10/Jun/2012 15:32:27] "GET /hello HTTP/1.1" 404 -
1.0.0.127.in-addr.arpa - - [10/Jun/2012 15:41:57] "GET /ss HTTP/1.1" 404 -
Run Code Online (Sandbox Code Playgroud)

我只想显示:你好ss

有什么办法可以做到吗?

linux grep

12
推荐指数
3
解决办法
5万
查看次数

使用BeautifulSoup获取属性的值

我正在编写一个python脚本,它将在从网页解析后提取脚本位置.可以说有两种情况:

<script type="text/javascript" src="http://example.com/something.js"></script>
Run Code Online (Sandbox Code Playgroud)

<script>some JS</script>
Run Code Online (Sandbox Code Playgroud)

我可以从第二个场景中获取JS,也就是在标签内部编写JS时.

但是有什么办法,我可以从第一个场景中获取src的值(即在脚本中提取src标签的所有值,例如http://example.com/something.js)

这是我的代码

#!/usr/bin/python

import requests 
from bs4 import BeautifulSoup

r  = requests.get("http://rediff.com/")
data = r.text
soup = BeautifulSoup(data)
for n in soup.find_all('script'):
    print n 
Run Code Online (Sandbox Code Playgroud)

输出:一些JS

需要输出:http://example.com/something.js

python beautifulsoup python-2.7

8
推荐指数
2
解决办法
2万
查看次数

通过Python执行Android命令并将结果存储在列表中

我正在通过Python执行ADB命令,它在某种程度上工作正常.代码是:

#!/usr/bin/python
import sys
import string
import os
import subprocess

cmd = 'adb shell ls'
s = subprocess.Popen(cmd.split())
print "Again"
t = str(s)
for me in t.split('\n') :
    print "Something"
    print me[1]
Run Code Online (Sandbox Code Playgroud)

我得到的输出是:

static-243:Scripts adityagupta$ ./hellome.py 
Again
Something
s
static-243:Scripts adityagupta$ config
cache
sdcard
acct
mnt
vendor
d
etc
ueventd.rc
ueventd.goldfish.rc
system
sys
sbin
proc
init.rc
init.goldfish.rc
init
default.prop
data
root
dev
Run Code Online (Sandbox Code Playgroud)

任何建议我可以使每个列表并存储其中的每个元素.列表应该是这样的

list = [cache,sdcard,acct,mnt,vendor ..]等等.

python android

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

python ×2

android ×1

beautifulsoup ×1

grep ×1

linux ×1

python-2.7 ×1