سؤال

Here's the URL first:

http://www.amazon.in/gp/product/B00EYCBFDQ/ref=s9_pop_gw_g147_i3?pf_rd_m=A1VBAL9TL5WCBF&pf_rd_s=center-3&pf_rd_r=1YP3T548XBFHJ1RA3EH8&pf_rd_t=101&pf_rd_p=402518447&pf_rd_i=1320006031

Above is the link to some product page on www.amazon.in.I want to get the actual price which is Rs.4,094. Below is a python code which tries to print the price and I have used //span[@id="actualPriceValue"]/text() to get the price but its returning an empty list.Can anyone suggest how to get the price?

from lxml import html
import requests

page = requests.get('http://www.amazon.in/gp/product/B00EYCBFDQ/ref=s9_pop_gw_g147_i3?pf_rd_m=A1VBAL9TL5WCBF&pf_rd_s=center-3&pf_rd_r=1YP3T548XBFHJ1RA3EH8&pf_rd_t=101&pf_rd_p=402518447&pf_rd_i=1320006031')
tree = html.fromstring(page.text)
price = tree.xpath('//span[@id="actualPriceValue"]/text()')

print price
هل كانت مفيدة؟

المحلول

Use the following XPath:

price = tree.xpath("//*[@id='actualPriceValue']/b/span/text()")[0]

The following code checks out:

from lxml import html
import requests

page = requests.get('http://www.amazon.in/gp/product/B00EYCBFDQ/ref=s9_pop_gw_g147_i3?pf_rd_m=A1VBAL9TL5WCBF&pf_rd_s=center-3&pf_rd_r=1YP3T548XBFHJ1RA3EH8&pf_rd_t=101&pf_rd_p=402518447&pf_rd_i=1320006031')
tree = html.fromstring(page.text)
price = tree.xpath("//*[@id='actualPriceValue']/b/span/text()")[0]

print price

Result:

4,094.00
[Finished in 3.0s]

Let us know if this helps.

نصائح أخرى

I think the issue is that the span with id actualPriceValue doesn't have direct text. You'll want to do something like this (I am pulling it out of my head so you may have to alter it):

Edit: Fixed. Below explanation is still accurate.

//*[@id='actualPriceValue']/b/span/text()

You'll notice the HTML looks like this:

<span id="actualPriceValue">
    <b class="priceLarge">
       <span style="text-decoration: inherit; white-space: nowrap;">
           <span class="currencyINR">&nbsp;&nbsp;</span>
           <span class="currencyINRFallback" style="display:none">Rs. </span>
           4,112.00
       </span>
    </b>
</span>

You'll notice that it should be:

Span with an id of actualPriceValue -> first b element -> first span element -> text
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top