HTML에서 데이터를 추출하는 쉬운 방법

HTML에서 데이터를 추출하는 쉬운 방법

웹페이지를 검색할 때

curl -X POST http://example.com/data/123

나는 다음과 같은 응답을 받았습니다.

<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>

위의 응답에서 태그 없이 모든 하위 도메인을 하나씩 가져오고 싶습니다. 예를 들면 다음과 같습니다.

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

답변1

당신은 그것을 사용할 수 있습니다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

답변2

당신은 그것을 사용할 수 있습니다 awk:

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

산출:

help.domain.com
hello.domain.com
test.domain.com

답변3

어쩌면 시도해봐lynx

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

고양이 파일.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

답변4

이것이 일회성 작업이라면 다른 답변도 괜찮을 수 있습니다.

그 밖의 모든 경우에는 적절한 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.

관련 정보