python scrapy无法从每个div获得每个班级名称的价值
我正在尝试从每个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,所以我解决了我的问题。基本上,我只需要在图标容器内部迭代并以其名称区分每个图标。
在循环环内循环。 :)
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. :)