首页 > 解决方案 > lxml-html 解析非常深的标签级python

问题描述

我需要使用 lxml python 模块从 html 标签获取图像源。 基本上我请求的网址是 - https://aeroparque.shopdutyfree.com/en/catalogsearch/result/?q=chocolate ,我只需要使用 lxml 获取任何产品图片的图像源。我的代码:

import requests
import lxml.html

.xpath('//a[@class="product photo product-item-photo"]/img[@class="product-image-photo"]/@src/text()')
for i in image:
        print(i)

标签: pythonhtmlparsinglxml

解决方案


/@src返回src属性的值,这是一个简单的字符串。添加/text()不会返回任何内容,因为text()从元素中提取文本,而不是属性。


推荐阅读