价格是在您在浏览器中加载页面时在执行 javascript 的帮助下构建的。想要的价格实际上是在script 标签内,您可以使用 Scrapy 定位该标签,使用正则表达式提取包含价格的对象,通过 JSON 加载并获取价格。
来自Scrapy Shell的演示:
$ scrapy shell http://www.bedstore.co.uk/p/Diamante_Faux_Leather_Bed_Frame.htm
In [1]: import re
In [2]: import json
In [3]: pattern = re.compile(r"qubit_product_list = (.*?);", re.M)
In [4]: script = response.xpath("//script[contains(., 'qubit_product_list')]/text()").extract()[0]
In [5]: data = pattern.search(script).group(1)
In [6]: json.loads(data)
Out[6]:
{u'6516-DBL-BLK': {u'category': u'',
u'currency': u'GBP',
u'description': u'Double Black',
u'id': u'6516',
u'name': u'Diamante Faux Leather Bed Frame',
u'sku_code': u'LPDF:DIAMANTE-BD',
u'stock': 100,
u'unit_price': 129.99,
u'unit_sale_price': 129.99,
u'url': u'/p/Diamante_Faux_Leather_Bed_Frame.htm'},
u'6516-DBL-WHT': {u'category': u'',
u'currency': u'GBP',
u'description': u'Double White',
u'id': u'6516',
u'name': u'Diamante Faux Leather Bed Frame',
u'sku_code': u'LPDF:DIAMANTE-WD',
u'stock': 100,
u'unit_price': 129.99,
u'unit_sale_price': 129.99,
u'url': u'/p/Diamante_Faux_Leather_Bed_Frame.htm'},
u'6516-KS-BLK': {u'category': u'',
u'currency': u'GBP',
u'description': u'Kingsize Black',
u'id': u'6516',
u'name': u'Diamante Faux Leather Bed Frame',
u'sku_code': u'LPDF:DIAMANTE-BK',
u'stock': 99,
u'unit_price': 149.99004,
u'unit_sale_price': 149.99004,
u'url': u'/p/Diamante_Faux_Leather_Bed_Frame.htm'},
u'6516-KS-WHT': {u'category': u'',
u'currency': u'GBP',
u'description': u'Kingsize White',
u'id': u'6516',
u'name': u'Diamante Faux Leather Bed Frame',
u'sku_code': u'LPDF:DIAMANTE-WK',
u'stock': 100,
u'unit_price': 154.98996,
u'unit_sale_price': 154.98996,
u'url': u'/p/Diamante_Faux_Leather_Bed_Frame.htm'}}