使用 xpath 从属性中提取 属性 id
Extract property id from attribute using xpath
我一直在尝试从以下网站提取 属性 id:https://www.kwsouthafrica.co.za/Property/RouteUrl?ids=P22%2C&ForSale=ForSale&PropertyTypes=&Beds=Any&Baths=Any&MinPrice=Any&MaxPrice=Any
但无论我尝试使用哪种组合,我似乎都无法检索它。
属性 ID 位于此处:
<div class="corner-ribbon">
<span class="ribbon-green">NEW!</span>
</div>
<a href="Details?id=182519" title="view this property">
<img class="img-responsive img-prop" src="https://kwsadocuments.blob.core.windows.net/devblob/24c21aa4-ae17-41d1-8719-5abf8f24c766.jpg" alt="Living close to Nature">
</a>
这是我目前尝试过的方法:
response.xpath('//a[@title="view this property"]/@href').getall(),
response.xpath('//*[@id="divListingResults"]/div/div/a/@href').getall(),
response.xpath('//*[@class="corner-ribbon"]/a/@href').getall()
对我可能做错了什么有什么建议吗?
提前致谢!
首先您需要了解此页面的工作原理。它使用 Javascript 加载属性(使用 Ctrl+U
在浏览器中检查页面源代码)并且(如你所知)Scrapy 无法处理 Javascript.
但是如果您检查页面源代码,您会发现您需要的所有信息都“隐藏”在 <input id="propertyJson" name="ListingResults.JsonResult" >
标签中。所以你只需要得到 value
并使用 json
模块处理它:
import scrapy
import json
class PropertySpider(scrapy.Spider):
name = 'property_spider'
start_urls = ['https://www.kwsouthafrica.co.za/Property/RouteUrl?ids=P22%2C&ForSale=ForSale&PropertyTypes=&Beds=Any&Baths=Any&MinPrice=Any&MaxPrice=Any']
def parse(self, response):
property_json = response.xpath('//input[@id="propertyJson"]/@value').get()
# with open('Samples/Properties.json', 'w', encoding='utf-8') as f:
# f.write(property_json)
property_data = json.loads(property_json)
for property in property_data:
property_id = property['Id']
property_title = property['Title']
print(property_id)
print(property_data)
我一直在尝试从以下网站提取 属性 id:https://www.kwsouthafrica.co.za/Property/RouteUrl?ids=P22%2C&ForSale=ForSale&PropertyTypes=&Beds=Any&Baths=Any&MinPrice=Any&MaxPrice=Any
但无论我尝试使用哪种组合,我似乎都无法检索它。
属性 ID 位于此处:
<div class="corner-ribbon">
<span class="ribbon-green">NEW!</span>
</div>
<a href="Details?id=182519" title="view this property">
<img class="img-responsive img-prop" src="https://kwsadocuments.blob.core.windows.net/devblob/24c21aa4-ae17-41d1-8719-5abf8f24c766.jpg" alt="Living close to Nature">
</a>
这是我目前尝试过的方法:
response.xpath('//a[@title="view this property"]/@href').getall(),
response.xpath('//*[@id="divListingResults"]/div/div/a/@href').getall(),
response.xpath('//*[@class="corner-ribbon"]/a/@href').getall()
对我可能做错了什么有什么建议吗? 提前致谢!
首先您需要了解此页面的工作原理。它使用 Javascript 加载属性(使用 Ctrl+U
在浏览器中检查页面源代码)并且(如你所知)Scrapy 无法处理 Javascript.
但是如果您检查页面源代码,您会发现您需要的所有信息都“隐藏”在 <input id="propertyJson" name="ListingResults.JsonResult" >
标签中。所以你只需要得到 value
并使用 json
模块处理它:
import scrapy
import json
class PropertySpider(scrapy.Spider):
name = 'property_spider'
start_urls = ['https://www.kwsouthafrica.co.za/Property/RouteUrl?ids=P22%2C&ForSale=ForSale&PropertyTypes=&Beds=Any&Baths=Any&MinPrice=Any&MaxPrice=Any']
def parse(self, response):
property_json = response.xpath('//input[@id="propertyJson"]/@value').get()
# with open('Samples/Properties.json', 'w', encoding='utf-8') as f:
# f.write(property_json)
property_data = json.loads(property_json)
for property in property_data:
property_id = property['Id']
property_title = property['Title']
print(property_id)
print(property_data)