Простой способ извлечения данных из HTML

Это можно сделать одной командойawk:

awk '$5 ~ /^sshd/ && $6 == "Connection"' log.txt
-2
21.04.2020, 04:27
4 ответа

вы можете использоватьsed

$ cat test

<td><a href="http://help.domain.com " target="_blank">help.domain.com</a></td>
<td><a href="http://hello.domain.com " target="_blank">hello.domain.com</a></td>
<td><a href="http://test.domain.com " target="_blank">test.domain.com</a></td>

$ sed 's/^.*">//;s/<.*//' test

help.domain.com
hello.domain.com
test.domain.com
0
19.03.2021, 02:27

Вы можете использоватьawk:

awk -F'">|</' '{ print $2 }' file

Выход:

help.domain.com
hello.domain.com
test.domain.com
0
19.03.2021, 02:27

Может попробоватьlynx

lynx -dump -listonly -nonumbers  http://example.com/data/123 | awk -F'[/:]+' '{print $2}'

cat файл.html

<td><a href="http://help.example.com " target="_blank">help.example.com</a></td>
<td><a href="http://hello.example.com " target="_blank">hello.example.com</a></td>
<td><a href="http://test.example.com " target="_blank">test.example.com</a></td>
lynx -dump -listonly -nonumbers  file.html | awk -F'[/:]+' '{print $2}'

Выход

help.example.com
hello.example.com
test.example.com
0
19.03.2021, 02:27

Если это одноразовая -задача, другие ответы, вероятно, подойдут.

Для всего остального используйте подходящий парсер xml или html!

.:BeautifulSoup:

curl -X POST http://example.com/data/123 | python -c '
from bs4 import BeautifulSoup
import sys
soup=BeautifulSoup(sys.stdin,"lxml")
for a in soup.find_all("a"):
  print(a.string)
'

Выход:

help.example.com
hello.example.com
test.example.com

вам может потребоваться установить bs4через pip.

Конечно, вам не нужно curl, когда вы запрашиваете страницу напрямую из python.

0
19.03.2021, 02:27

Теги

Похожие вопросы