gpt4 book ai didi

python - xpath 为什么我在这个expth中得到空结果

转载 作者:行者123 更新时间:2023-11-30 23:14:20 25 4
gpt4 key购买 nike

我尝试这个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,我可以查看这样的响应:

scrapy shell ("website")
view(response)

那个类就在那里。

请帮忙

来 self 的 Chrome 的使用 View (响应)的页面的屏幕截图

Screenshot from my Chrome for the page that comes using view(response)

最佳答案

问题是:包含带有 owl-wrapper 类的 div 元素的搜索结果通过额外的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com