CSS选择器还是在两个i标记之间获取信息的XPath?

2022-02-25 00:00:00 xpath web-scraping scrapy web-crawler css

我正在尝试抓取价格信息,网站的HTML如下所示

<span class="def-price" datasku='....'>
   <i>$</i>
   "999"
   <i>.00<i>
</span>

我想要999。(我不想要美元符号或.00)我目前有

product_price_sn = product.css('.def-price i').extract()

我知道这是错误的,但不确定如何修复它。你知道怎么弄到价格信息吗?谢谢!


解决方案

您可以使用此//span[@class="def-price"]/text()

确保您使用的是/text(),而不是//text()。否则,它将返回span标记内的所有文本节点。

此CSS选择器.def-price::text。使用CSS选择器时,不要使用.def-price ::text,它将返回所有文本节点,如XPath中的//text()

使用scrapy响应.xpath对象

from scrapy.http import Request, HtmlResponse as Response

content = '''<span class="def-price" datasku='....'>
   <i>$</i>
   "999"
   <i>.00<i>
</span>'''.encode('utf-8')

url = 'https://stackoverflow.com/questions/62849500'

''' mocking scrapy request object '''
request = Request(url=url)

''' mocking scrapy response object '''
response = Response(url=url, request=request, body=content)

''' using xpath  '''

print(response.xpath('//span[@class="def-price"]/text()').extract())
# outputs ['
   ', '
   "999"
   ']

print(''.join(response.xpath('//span[@class="def-price"]/text()').extract()).strip())
# outputs "99"

''' using css selector '''

print(response.css('.def-price::text').extract())
# outputs ['
   ', '
   "999"
   ']

print(''.join(response.css('.def-price::text').extract()).strip())
# outputs "99"

实际查看here

使用lxml html解析器

from lxml import html

parser = html.fromstring("""
<span class="def-price" datasku='....'>
   <i>$</i>
   "999"
   <i>.00<i>
</span>
"""
)

print(parser.xpath('//span[@class="def-price"]/text()'))
# outputs ['
   ', '
   "999"
   ']

print(''.join(parser.xpath('//span[@class="def-price"]/text()')).strip())
# outputs "999"

实际查看here

相关文章