2010-02-02 3 views
1

소켓에 연결하고 .read()를 사용하여 응답을 캡처 한 후 입력 스트림을 구문 분석하고 회선을 읽는 방법은 무엇입니까?httplib을 사용하여 파이썬 응답 구문 분석

나는 데이터가 어떤 CRLF

<html><head><title>Apache Tomcat/6.0.16 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 404 - /index.html</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>/index.html</u></p><p><b>description</b> <u>The requested resource (/index.html) is not available.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/6.0.22</h3></body></html> 
+2

코드를 보여주세요! – ghostdog74

+0

그리고 구문 분석을 원하십니까? – ghostdog74

+0

아마도 출력이 오류 메시지 인 것 같아서 read()가 제대로 작동하지 않을 수도 있습니다. –

답변

0

하는 HTML 파서를 사용하지 않고 반환을 참조하십시오. Beautiful Soup가 인기있는 것 같습니다.

+0

downmodder : 당신의 투표에 대해 설명해 주시겠습니까? – danben

3

HTML을 구문 분석해야합니다. 파이썬은 HTML을 파싱하는 몇 가지 방법을 가지고있다. 하나는 내장 된 HTMLParser 모듈이다. 또 다른, 그리고 아마도 더 좋은 방법은 타사 BeautifulSoup 모듈입니다.

HTML 처리와 관련된 다른 많은 문제는이 nice article에 설명되어 있습니다. (무료 온라인) Dive into Python 도서 relevant chapter을 읽을 수도 있습니다.

+0

왜 이것이 다운 몰드 되었습니까? –