我尝试这个xpath

.//div[@class='owl-wrapper']


在这个网站上

http://www.justproperty.com/search/uae/apartments/filter__cid/0/sort/score__desc/per_page/20/page/1

但是我得到了空结果,尽管我可以在Google F12开发人员工具中看到它。

您可能会认为这是一个javascript调用,但这不是因为我正在使用scrapy并且可以view这样的响应:

scrapy shell ("website")
view(response)


那堂课在那里。

请帮助

我的Chrome中使用视图的页面截图(响应)

最佳答案

问题是:包含div元素和owl-wrapper类的搜索结果将与其他GET请求异步加载。

您需要在代码中模拟此请求,例如使用requests

import requests

with requests.Session() as session:
    session.get('http://www.justproperty.com/search/uae/apartments/filter__cid/0/sort/score__desc/per_page/20/page/1')

    params = {
        'url': 'filter__cid/0/sort/score__desc/per_page/20/page/1',
        'ajax': 'true'
    }
    response = session.get('http://www.justproperty.com/search/featured-properties/', params=params)
    results = response.json()

    for result in results:
        print result['description']


印刷品:

2 bedroom unit on high floor. Full Fountain View,It comes with different amenities, facilities and hotel services. It is located in a prime location, The Address Hotel Lake Downtown. This property is...
Large Upgraded 1 Bedroom For Sale In Index Tower DIFC With DIFC ViewSize: 840 square feet - 78 square metersBedroom: 1 Bathroom: 1 plus guest washroomKitchen: Fully Equipped modern style kitchen with...
Spacious and nice 1-bedroom apartment for
...




基于上述提供的解决方案的示例Scrapy蜘蛛:

import json

import scrapy


class JustPropertySpider(scrapy.Spider):
    name = "justproperty"
    allowed_domains = ["justproperty.com"]
    start_urls = [
        "http://www.justproperty.com/search/uae/apartments/filter__cid/0/sort/score__desc/per_page/20/page/1"
    ]

    def parse(self, response):
        yield scrapy.Request('http://www.justproperty.com/search/featured-properties/?url=filter__cid/0/sort/score__desc/per_page/20/page/1&ajax=true',
                             callback=self.parse_results,
                             headers={'X-Requested-With': 'XMLHttpRequest'})

    def parse_results(self, response):
        results = json.loads(response.body)

        for result in results:
            print result['description']

关于python - xpath为什么我在此expth中得到空结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28810066/

10-10 18:47
查看更多