2014-05-01 19 views
1

我正在使用scrapy抓取網站。一切工作都很好,直到我遇到了這個問題的兩個值。Scrapy抓取:無法將數據解析爲人類可讀的值

這裏是我取的價值

hxs.select("//table[@class='bodypad']//table/tr[1]/td//tr[10]//td[2]/text()").extract()[0].strip() 

以下爲輸出

u'Rs.\xa05,000\n\r\n\t\t\t\t\t/-' 

我還可以看到帶()方法也不能用於該值。以下是我的代碼,其中該代碼是工作的罰款部分

hxs.select("//table[@class='bodypad']//table/tr[1]/td//tr[10]//td[2]/text()").extract()[2] 

輸出:

u'Rs. 1,000' 

,當我使用.encode(「ASCII」)我得到正是我需要的:

'Rs. 1,000' 

你可以建議我該怎麼做才能獲得第一個值,在網站上看起來像是Rs。 5,000/-。我想得到類似的東西,.encode('ascii')不能用於第一個值。

編輯 - 樣本HTML輸入

<table width="100%" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> 
      <tbody><tr class="table_bdrow1_style"> 
      <td width="40%" class="table_header_style"><b>Minimum Initial Investment</b></td> 
      <td class="table_bdtext_style"> 
       Rs.&nbsp;5,000 

       /- 
      </td> 
      </tr> 
      <tr class="table_bdrow1_style"> 
      <td width="40%" class="table_header_style"><b>Minimum Subsequent Investment</b></td> 
      <td class="table_bdtext_style"> 
       Rs.&nbsp;1,000 

        /- 

        </td> 
      </tr>     

      <!-- 
      <tr class="table_bdrow1_style"> 
      <td width="40%" class=table_header_style><b>Minimum RSP Investment</b></td> 
     -->  
      <!--<td class=table_bdtext_style width="55%">-</td>--> 
      <!-- 
      <td class=table_bdtext_style>-</td> 
      </tr> 
      --> 
     <tr class="table_bdrow1_style"> 

      <td width="40%" class="table_header_style"><b>Minimum Redemption Amount</b></td> 
      <td class="table_bdtext_style">Rs. 1,000</td>   
     </tr> 
     <!-- 
     <tr class="table_bdrow1_style"> 
      <td width="40%" class=table_header_style valign="top"><b>Minimum Holding</b></td> 
      <td class=table_bdtext_style>- 
      </td> 
     </tr> 
     <tr class="table_bdrow1_style"> 
      <td width="40%" class=table_header_style><b>Cooling-off Period</b></td> 
      <td class=table_bdtext_style>-</td> 
     </tr> 
     --> 
     <tr class="table_bdrow1_style"> 
      <td width="40%" class="table_header_style"><b>Minimum Holding Period</b></td> 
      <td class="table_bdtext_style">-</td> 
     </tr> 
     <tr class="table_bdrow1_style"> 
      <td width="40%" class="table_header_style"><b>Transaction Time for Redemption</b></td> 

      <td class="table_bdtext_style">1:50 PM</td> 
     </tr> 
     <tr class="table_bdrow1_style"> 
      <td width="40%" class="table_header_style"><b>Entry Load</b></td> 
      <td class="table_bdtext_style">-</td> 
     </tr> 
     <tr class="table_bdrow1_style"> 
      <td width="40%" class="table_header_style"><b>Exit Load</b></td> 
      <td class="table_bdtext_style">0.25% if the investments is redeemed/switched out within 1 month form the date of allotment 
      </td> 
     </tr> 
     </tbody></table>` 
+0

你能發佈一些樣本HTML輸入? –

+0

我在我的問題結尾處添加了一些示例html。 –

+0

給'strip()'給出了一個以連字符結尾的字符串,由於它不是空格,因此不會被刪除。也許你想把字符串分成幾行,取第一個,然後去掉? –

回答

1

\xa0Non-breaking space其顯示爲一個網頁一個簡單的空間。是代碼A0這是ASCII範圍(0-127)以外:

Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
>>> u'Rs.\xa05,000\n\r\n\t\t\t\t\t/-'.encode() 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 3: ordinal not in range(128) 

所以,你必須用一個簡單的空間來手動替換它,它編碼爲ASCII之前。

默認str.strip只剝掉空格,所以你應該手動去掉字符/ -

這應該工作:

>>> u'Rs.\xa05,000\n\r\n\t\t\t\t\t/-'.replace(u'\xa0', u' ').encode().rstrip('-/ ').strip() 
'Rs. 5,000' 
>>>