Chinaunix

标题: 关于XML和HTML的提取值的请教 [打印本页]

作者: qingyuan0o0    时间: 2013-09-26 00:00
标题: 关于XML和HTML的提取值的请教
本帖最后由 qingyuan0o0 于 2013-09-26 16:06 编辑

我在网上搜索了关于很多提取值的代码,虽然我复制下来都能得到结果,但是却看不懂,不知道怎么修改成自己需要的代码


<postfield name="backUrl" value=""/><postfield name="touchTitle" value=""/><postfield name="touchTextLength" value=""/><postfield name="msg" value="$(msg1380120040127)"/><postfield name="csrfToken" value="b30bd351371c686298d32281b337e8e9"/></go></anchor><br/> 添加&gt;<anchor>表情<go method="post" href="/im/chat/insertPic.action?

代码中csrfToken是唯一的,我想要提取他的值b30bd351371c686298d32281b337e8e9(随机的每次都会变化),而不能根据value,因为代码类似的太多,值又不一样。

根据类似的代码,我找到一个<meta name="description" content="shell如何获取html页面的title和description里的内容"/>类似的

因为看不懂,我没办法模仿,实现语句是cat renren.html | grep -i 'description' | sed -e 's/<[^<]*=\"//g' | sed -e 's/"\ \/>$//g'

只是想知道我应该如何去获取,如果只是用grep的方式,我应该怎么改

csrfToken= `grep -Eo  '<postfield name="csrfToken"(.*?)' test | grep -Eo  'video=\w+`

按照大神在第一页说的
  1. <a href="/im/user/userinfoByuserid.action?touserid=123451047&amp;t=1380172127438">缘</a>[在线]&gt;<a href="/im/index/recentcontacts.action?touserid=123451047">换人</a><br/> [新]测试1[13:08]<br/> [新]测试2[13:08]<br/><input name="msg1380172127371" title="" value="" type="text" emptyok="true" maxlength="1000"/><br/><anchor>发送消息 <go method="post" href="/im/chat/sendMsg.action?touserid=123451047" accept-charset="utf-8"><postfield name="backUrl" value=""/><postfield name="touchTitle" value=""/><postfield name="touchTextLength" value=""/><postfield name="msg" value="$(msg1380172127371)"/><postfield name="csrfToken" value="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"/></go></anchor><br/> 添加&gt;<anchor>表情<go method="post" href="/im/chat/insertPic.action?touserid=123451047" accept-charset="utf-8"><postfield name="backUrl" value=""/><postfield name="touchTitle" value=""/><postfield name="touchTextLength" value=""/><postfield name="msg" value="$(msg1380172127371)"/></go></anchor>.<anchor>动作<go method="post" href="/im/chat/insertTouch.action?touserid=123451047" accept-charset="utf-8"><postfield name="backUrl" value=""/><postfield name="msg" value="$(msg1380172127371)"/></go></anchor>.<anchor>招呼<go method="post" href="/im/chat/sayHelloToFriend.action?touserid=123451047&amp;backUrl=" accept-charset="utf-8"></go></anchor><br/> ------------<br/> [13:06] 缘:测试<anchor>转<go method="post" href="/im/sayHello/sayHelloChooseFriendsByAll.action"><postfield name="type" value="all"/><postfield name="sayHelloText" value="测试"/><postfield name="fromSelect" value="fromSelectAll"/><postfield name="selectType" value="all"/></go></anchor><br/> [12:52] 我:hh<anchor>转<go method="post" href="/im/sayHello/sayHelloChooseFriendsByAll.action"><postfield name="type" value="all"/><postfield name="sayHelloText" value="hh"/><postfield name="fromSelect" value="fromSelectAll"/><postfield name="selectType" value="all"/></go></anchor><br/> (共1页)
复制代码
页面接收到消息会有提示一个[新]和后面的[13:08]时间,再一次刷新,就会变成下面的[13:06]对方用户名:内容到下面来了<anchor>转

而用'/新/!d 就变成包含新的行不删除,但是在wget下来后,所有内容就只是在第一行,并没有换行我应该怎么,一字不漏的把它记录下来,然后15秒后再刷新?

提取类似的这3部分,一个是,然后是按照时间先后顺序[13:08]排序内容测试1
[13:08] :测试1
[13:09] :测试2
每次内容变动后就按照时间递增
求大家帮忙
作者: reb00t    时间: 2013-09-26 00:22
回复 1# qingyuan0o0


     grep -Eo  '<postfield name="csrfToken"(.*?)' c.txt |grep -oP '(?<=value=")[^"]+'
作者: Shell_HAT    时间: 2013-09-26 08:24
  1. sed -r '/csrfToken/!d; s/.*csrfToken" value="([^"]+)".*/\1/' urfile
复制代码

作者: qingyuan0o0    时间: 2013-09-26 10:29
感谢楼上两位大哥帮忙,我测试过,第一句,会出现3个内容,包括了$(msg1380120040127) $(msg1380120040127)
作者: LikeLx    时间: 2013-09-26 10:42
回复 4# qingyuan0o0
你测试第一句的时候是不是把那个c.txt忘了 ?

   
作者: Shell_HAT    时间: 2013-09-26 10:43
回复 5# LikeLx


value="([^"]+)".*    高手,观察你用了很多次这样的写法,不是太明白,是表示以“开头后接一个或以上的"吗 ?

^在[]里面表示
作者: LikeLx    时间: 2013-09-26 10:46
回复 6# Shell_HAT
多谢指点。

   
作者: LikeLx    时间: 2013-09-26 10:50
回复 6# Shell_HAT
高手,还想问下'/csrfToken/!d 这句是表示/csrfToken/之前的内容都删除吗?

   
作者: Shell_HAT    时间: 2013-09-26 11:07
回复 8# LikeLx


    包含csrfToken的行不删
作者: jackyyen    时间: 2013-09-26 11:12
awk可以处理呀
作者: HH106    时间: 2013-09-26 11:16
  1. #!/usr/bin/env python
  2. from BeautifulSoup import BeautifulSoup
  3. text=open("txt").read()
  4. soup=BeautifulSoup(''.join(text))
  5. print soup('postfield',{"name":"csrfToken"})[0]['value']
复制代码

作者: qingyuan0o0    时间: 2013-09-26 11:31
回复 11# HH106


    我想说的是路由器系统太小,没有安装python
作者: reb00t    时间: 2013-09-26 11:34
回复 12# qingyuan0o0


    grep -Eo  '</img><a href="/im/chat/toinputMsg.action(.*?)' c.txt |sed  -r 's/.*touserid=([0-9]+).*/\1/'
作者: Shell_HAT    时间: 2013-09-26 11:38
  1. sed -r '/touserid/!d; s/.*touserid=([0-9]+).*/\1/' urfile
复制代码

作者: reb00t    时间: 2013-09-26 11:40
回复 15# Shell_HAT


    拜大神
作者: qingyuan0o0    时间: 2013-09-26 13:20
本帖最后由 qingyuan0o0 于 2013-09-26 16:06 编辑

按照大神在第一页说的
  1. <a href="/im/user/userinfoByuserid.action?touserid=123451047&amp;t=1380172127438">缘</a>[在线]&gt;<a href="/im/index/recentcontacts.action?touserid=123451047">换人</a><br/> [新]测试1[13:08]<br/> [新]测试2[13:08]<br/><input name="msg1380172127371" title="" value="" type="text" emptyok="true" maxlength="1000"/><br/><anchor>发送消息 <go method="post" href="/im/chat/sendMsg.action?touserid=123451047" accept-charset="utf-8"><postfield name="backUrl" value=""/><postfield name="touchTitle" value=""/><postfield name="touchTextLength" value=""/><postfield name="msg" value="$(msg1380172127371)"/><postfield name="csrfToken" value="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"/></go></anchor><br/> 添加&gt;<anchor>表情<go method="post" href="/im/chat/insertPic.action?touserid=123451047" accept-charset="utf-8"><postfield name="backUrl" value=""/><postfield name="touchTitle" value=""/><postfield name="touchTextLength" value=""/><postfield name="msg" value="$(msg1380172127371)"/></go></anchor>.<anchor>动作<go method="post" href="/im/chat/insertTouch.action?touserid=123451047" accept-charset="utf-8"><postfield name="backUrl" value=""/><postfield name="msg" value="$(msg1380172127371)"/></go></anchor>.<anchor>招呼<go method="post" href="/im/chat/sayHelloToFriend.action?touserid=123451047&amp;backUrl=" accept-charset="utf-8"></go></anchor><br/> ------------<br/> [13:06] 缘:测试<anchor>转<go method="post" href="/im/sayHello/sayHelloChooseFriendsByAll.action"><postfield name="type" value="all"/><postfield name="sayHelloText" value="测试"/><postfield name="fromSelect" value="fromSelectAll"/><postfield name="selectType" value="all"/></go></anchor><br/> [12:52] 我:hh<anchor>转<go method="post" href="/im/sayHello/sayHelloChooseFriendsByAll.action"><postfield name="type" value="all"/><postfield name="sayHelloText" value="hh"/><postfield name="fromSelect" value="fromSelectAll"/><postfield name="selectType" value="all"/></go></anchor><br/> (共1页)
复制代码
页面接收到消息会有提示一个[新]和后面的[13:08]时间,再一次刷新,就会变成下面的[13:06]对方用户名:内容到下面来了<anchor>转

而用'/新/!d 就变成包含新的行不删除,但是在wget下来后,所有内容就只是在第一行,并没有换行我应该怎么,一字不漏的把它记录下来,然后15秒后再刷新?

提取类似的这3部分,一个是,然后是按照时间先后顺序[13:08]排序内容测试1
[13:08] :测试1
[13:09] :测试2
每次内容变动后就按照时间递增

更新到第一页去了
作者: Shell_HAT    时间: 2013-09-26 13:34
回复 17# qingyuan0o0


    以这段数据为例,你希望得到的结果是什么?
作者: qingyuan0o0    时间: 2013-09-26 13:39
本帖最后由 qingyuan0o0 于 2013-09-26 13:53 编辑

回复 18# Shell_HAT


    我希望得到的结果是每次刷新后有[新]测试1[13:08] 然后提取出来值(测试1)能保存到log文件中持续增长
info=`sed -r '/新/!d; s/.*[新]" :"([^"]+)".*/\1/' urfile`

echo info >> log
log:

[13:08] 测试1
[13:09] 测试2
...
..
.
总之就类似由下而上这样提取
作者: Shell_HAT    时间: 2013-09-26 13:57
回复 19# qingyuan0o0
  1. sed 's/\[新\]/\n&/g' urfile | sed '/\[新\]/!d; s/.*新.\([^[]\+\)\(...:...\).*/\2 \1/g'
复制代码

作者: qingyuan0o0    时间: 2013-09-26 14:03
回复 20# Shell_HAT

谢谢大神,我编辑了一下16楼,麻烦看一下,为啥您能这么强悍~
    [13:08] :测试1
[13:09] :测试2

作者: Shell_HAT    时间: 2013-09-26 14:15
  1. strSplit=`sed 's/\[新\]/\n&/g' urfile`
  2. strName=`echo "$strSplit" | sed -r '1!d; s/[^>]+>([^<]+).*/\1/'`
  3. echo "$strSplit" | sed -n "s/.*新.\([^[]\+\)\(...:...\).*/\2 $strName:\1/p"
复制代码

作者: qingyuan0o0    时间: 2013-09-26 14:41
root@OpenWrt:~/tmp# sh cs
sed: bad option in substitution expression

不知道为啥,运行后报错
作者: Shell_HAT    时间: 2013-09-26 14:53
  1. #!/bin/bash
  2. strSplit=`sed 's/\[新\]/\n&/g' urfile`
  3. strName=`echo "$strSplit" | sed -r '1!d; s/[^>]+>([^<]+).*/\1/'`
  4. echo "$strSplit" | sed -n "s/.*新.\([^[]\+\)\(...:...\).*/\2 $strName:\1/p"
复制代码
chmod u+x test.sh
./test.sh
不要这样运行:sh test.sh
作者: qingyuan0o0    时间: 2013-09-26 14:55
回复 24# Shell_HAT


    谢谢大神提醒,但是我一般那样运行是无效的,所以我都这么运行
  1. root@OpenWrt:~/tmp# sh cs
  2. sed: bad option in substitution expression
  3. root@OpenWrt:~/tmp# chmod u+x cs
  4. root@OpenWrt:~/tmp# ./cs
  5. -ash: ./cs: not found
  6. root@OpenWrt:~/tmp# sh cs
  7. sed: bad option in substitution expression
  8. root@OpenWrt:~/tmp#
复制代码

作者: Shell_HAT    时间: 2013-09-26 14:59
回复 25# qingyuan0o0


你的系统里面有bash吗?
  1. bash test.sh
复制代码

作者: qingyuan0o0    时间: 2013-09-26 15:10
回复 26# Shell_HAT


    <a href="/im/user/userinfoByuserid.action?touserid=123451047&amp;t=1380179029159">缘</a>[在线]&gt;<a href="/im/index/recentcontacts.action?touserid=123451047">换人</a><br/> [新]1[15:01]<br/> [新]2[15:05]<br/> [新]3[15:06]<br/> [新]4[15:07]<br/> [新]5[15:08]<br/> [新]6[15:02]<br/> [新]7[15:03]<br/> [新]8[15:04]<br/> [新]9[15:09]<br/> [新]0[15:00]<br/>
  1. root@OpenWrt:/# bash
  2. bash
  3. sh: bash: not found
  4. root@OpenWrt:/# sh -v
  5. sh -v


  6. BusyBox v1.19.4 (2013-02-19 23:36:56 CST) built-in shell (ash)
  7. Enter 'help' for a list of built-in commands.

  8. root@OpenWrt:/#
复制代码

作者: Shell_HAT    时间: 2013-09-26 15:16
回复 27# qingyuan0o0


我这里没有BusyBox,没法测试。你逐行执行一下,看看具体哪行哪里在报错。
作者: qingyuan0o0    时间: 2013-09-26 15:19
回复 28# Shell_HAT


    我给你发私信了,里面可以登录我的ssh,我的小路由
作者: Shell_HAT    时间: 2013-09-26 15:22
回复 29# qingyuan0o0


    我在公司连不上,也许是网络限制,回家再试试。
作者: qingyuan0o0    时间: 2013-09-26 15:47
回复 30# Shell_HAT


    好的,麻烦你了,谢谢
作者: qingyuan0o0    时间: 2013-09-27 08:23
回复 30# Shell_HAT


    请问怎么样了?




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2