2010-02-02 28 views
1

連接到套接字並使用.read()捕獲響應後,如何解析輸入流並讀取行?使用httplib解析Python響應

我看到沒有任何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 2010-02-02 04:13:20

+0

它是什麼,你想解析? – ghostdog74 2010-02-02 04:19:34

+0

也許read()工作不正常,因爲輸出似乎是錯誤信息 – 2010-02-02 04:21:37

回答

0

使用HTML解析器返回的數據。 Beautiful Soup似乎是一個受歡迎的。

+0

對於downmodder:你是否在意解釋你的投票? – danben 2010-02-02 14:26:44

3

你必須解析HTML。 Python有幾種解析HTML的方式 - 其中之一是內置的HTMLParser模塊。另一種可能更好的方式是第三方模塊BeautifulSoup

在這個nice article中解釋了處理HTML處理的許多其他問題。您還可以閱讀(免費在線)Dive into Python書籍的relevant chapter

+0

我不知道爲什麼這是下降? – 2010-02-02 15:41:16