Pergunta

I am scraping a website using scrapy. Everything was working just fine untill I came accross this particular problem for a couple of values.

Here is how I fetch the value

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

Following is the output

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

I can also see that the strip() method is also not working for this value. Following is the part of my code where this code is working fine

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

Output:

u'Rs. 1,000'

and when I use a .encode('ascii') I get exactly what I need:

'Rs. 1,000'

Can you suggest me what to do to get the first value also, on the website it looks like Rs. 5,000 / - . I want to get something similar, also the .encode('ascii') is not working for the first value.

EDIT - Sample HTML INPUT

<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>`
Foi útil?

Solução

\xa0 is Non-breaking space which is shown as a simple space in a web-page. Is code is A0 which is outside of of ASCII range (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)

So you have to manually replace it with a simple space, before encoding it to ASCII.

Be default str.strip strips only whitespace, so you should manually strip chars / -.

This should work:

>>> u'Rs.\xa05,000\n\r\n\t\t\t\t\t / -'.replace(u'\xa0', u' ').encode().rstrip('-/ ').strip()
'Rs. 5,000'
>>> 
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top