Select CSS 选择器中的数量
Select amount in CSS selector
我正在尝试 select CSS select 或下一页的价格;
https://www.funda.nl/en/koop/nieuwegein/huis-42656543-wattbaan-22/
我想要select的css路径是'strong.object-header__price'。这对应scrapy中的下面一行代码shell并输出:
response.css('strong.object-header__price').xpath('normalize-space()').extract()
['€ 675,000 k.k.']
不过,我只想要select金额,67.5万。
对于 xpath 我知道如何做到这一点,即:
response.xpath("substring-before(substring(//
[@id='content']/div/div/div[1]/section[5]/div/dl[1]/dd[1]/span[1]/text(),'3','25'),'
')").extract()
有人可以告诉我如何执行相同的步骤,但是 css select 或者?我在互联网上找不到如何做到这一点,因此提出了这个问题。
提前致谢。
这里有几个选项:
1. Select 子字符串(可能是你想要的):
In [1]: price = response.css('.object-header__price::text').get()[2:-5]
In [2]: price
Out[2]: '675,000'
2. 使用替换:
In [1]: price = response.css('.object-header__price::text').get()
In [2]: price = price.replace('€ ', '')
In [3]: price = price.replace(' k.k.', '')
In [4]: price
Out[4]: '675,000'
3. 使用正则表达式
In [1]: import re
In [2]: price = response.css('.object-header__price::text').get()
In [3]: price = re.findall(r'(\d+,\d+)', price)
In [4]: price[0]
Out[4]: '675,000'
4. 从脚本中获取:
In [1]: import json
In [2]: price = response.css('head > script[type="application/ld+json"]::text').get()
In [3]: script_data = json.loads(price)
In [4]: script_data['offers']['price']
Out[4]: '675000'
我正在尝试 select CSS select 或下一页的价格;
https://www.funda.nl/en/koop/nieuwegein/huis-42656543-wattbaan-22/
我想要select的css路径是'strong.object-header__price'。这对应scrapy中的下面一行代码shell并输出:
response.css('strong.object-header__price').xpath('normalize-space()').extract()
['€ 675,000 k.k.']
不过,我只想要select金额,67.5万。
对于 xpath 我知道如何做到这一点,即:
response.xpath("substring-before(substring(//
[@id='content']/div/div/div[1]/section[5]/div/dl[1]/dd[1]/span[1]/text(),'3','25'),'
')").extract()
有人可以告诉我如何执行相同的步骤,但是 css select 或者?我在互联网上找不到如何做到这一点,因此提出了这个问题。
提前致谢。
这里有几个选项:
1. Select 子字符串(可能是你想要的):
In [1]: price = response.css('.object-header__price::text').get()[2:-5]
In [2]: price
Out[2]: '675,000'
2. 使用替换:
In [1]: price = response.css('.object-header__price::text').get()
In [2]: price = price.replace('€ ', '')
In [3]: price = price.replace(' k.k.', '')
In [4]: price
Out[4]: '675,000'
3. 使用正则表达式
In [1]: import re
In [2]: price = response.css('.object-header__price::text').get()
In [3]: price = re.findall(r'(\d+,\d+)', price)
In [4]: price[0]
Out[4]: '675,000'
4. 从脚本中获取:
In [1]: import json
In [2]: price = response.css('head > script[type="application/ld+json"]::text').get()
In [3]: script_data = json.loads(price)
In [4]: script_data['offers']['price']
Out[4]: '675000'