python scrapy无法从每个div获得每个班级名称的价值

发布于 2025-01-23 16:53:46 字数 1719 浏览 0 评论 0原文

我正在尝试从每个DIV列表中获取每个类名称均具有相应的值。

在此页面上: https:https:https:// www。 dotproperty.id/en/properties-for-sale/riau-islands/batam?page=1

这是班级名称和价值观的HTML:

<ul class="list-inline">
<li><i class="icon icon-bedroom"></i> <span>3</span></li>
<li><i class="icon icon-dp-icon-24"></i> <span>2</span></li>
<li><i class="icon icon-dp-icon-26"></i> <span>100 m<sup>2</sup></span></li>
</ul>

我最接近的是,我最接近的是它使用getall()并同步打印每个类,并并排。但是,它给了我整个页面。

for e in response.css('div.description-block'):
     for item in e.xpath('.//ul[@class="list-inline"]'):
         print(item.xpath('.//li/i/@class | .//li/span/text()').getall())

输出:

['icon icon-bedroom', '3', 'icon icon-dp-icon-24', '2', 'icon icon-dp-icon-26', '100 m']
['icon icon-bedroom', '2', 'icon icon-dp-icon-24', '1']
['icon icon-bedroom', '6', 'icon icon-dp-icon-24', '5', 'icon icon-dp-icon-26', '198 m']
['icon icon-bedroom', '4', 'icon icon-dp-icon-24', '5', 'icon icon-dp-icon-26', '450 m']
['icon icon-dp-icon-26', '336 m', ' ']

尝试get()给出每个Div的每个头等名称:

icon icon-bedroom
icon icon-bedroom
icon icon-bedroom
icon icon-bedroom
icon icon-dp-icon-26
(and so on ...)

我希望每个循环的输出看起来像这样。

['icon icon-bedroom', '3', 'icon icon-dp-icon-24', '2', 'icon icon-dp-icon-26', '100 m']

I'm trying to get each class names with its corresponding value from each div listing.

From this page: https://www.dotproperty.id/en/properties-for-sale/riau-islands/batam?page=1

Here's the html for the class names and values:

<ul class="list-inline">
<li><i class="icon icon-bedroom"></i> <span>3</span></li>
<li><i class="icon icon-dp-icon-24"></i> <span>2</span></li>
<li><i class="icon icon-dp-icon-26"></i> <span>100 m<sup>2</sup></span></li>
</ul>

So far the closest I got is that it prints each classes with its value side by side using getall(). But, it gives me the whole page.

for e in response.css('div.description-block'):
     for item in e.xpath('.//ul[@class="list-inline"]'):
         print(item.xpath('.//li/i/@class | .//li/span/text()').getall())

Output:

['icon icon-bedroom', '3', 'icon icon-dp-icon-24', '2', 'icon icon-dp-icon-26', '100 m']
['icon icon-bedroom', '2', 'icon icon-dp-icon-24', '1']
['icon icon-bedroom', '6', 'icon icon-dp-icon-24', '5', 'icon icon-dp-icon-26', '198 m']
['icon icon-bedroom', '4', 'icon icon-dp-icon-24', '5', 'icon icon-dp-icon-26', '450 m']
['icon icon-dp-icon-26', '336 m', ' ']

Trying get() gives each first class name of each div:

icon icon-bedroom
icon icon-bedroom
icon icon-bedroom
icon icon-bedroom
icon icon-dp-icon-26
(and so on ...)

I want the output to look like that for each loop.

['icon icon-bedroom', '3', 'icon icon-dp-icon-24', '2', 'icon icon-dp-icon-26', '100 m']

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独﹏钓一江月 2025-01-30 16:53:46

好的,所以我解决了我的问题。基本上,我只需要在图标容器内部迭代并以其名称区分每个图标。

在循环环内循环。 :)

for a in accommodation:
        class_name = a.xpath('.//i/@class').getall()
        value = a.xpath('li/span/text()').getall()
        for name,val in zip(class_name,value):
            if 'bedroom' in name:
                bedrooms = val
            else:
                pass
            if 'icon-24' in name:
                bathrooms = val
            else:
                pass
            if 'icon-26' in name:
                land = val
            else:
                pass

Ok, so i solved my issue. Basically, i just needed to iterate inside the icon container and discriminate each icon by its names.

Looping inside the looping loop. :)

for a in accommodation:
        class_name = a.xpath('.//i/@class').getall()
        value = a.xpath('li/span/text()').getall()
        for name,val in zip(class_name,value):
            if 'bedroom' in name:
                bedrooms = val
            else:
                pass
            if 'icon-24' in name:
                bathrooms = val
            else:
                pass
            if 'icon-26' in name:
                land = val
            else:
                pass
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文