我成功地使用我的技术获取了网址,但要指出的是,我需要稍微更改网址:“http://www.example.com/static/p/no-name-0330-227404-1.jpg”。凡在img标签我得到这个链接: “http://www.example.com/static/p/no-name-0330-227404-1-product.jpg”从网址中排除某些关键字
HTML代码:
<div class="swiper-wrapper"><img data-error-placeholder="PlaceholderPDP.jpg" class="swiper-lazy swiper-lazy-loaded" src="http://www.example.com/static/p/no-name-0330-227404-1-product.jpg"></div>
Python代码:
imagesList = []
imagesList.append([re.findall(re.compile(u'http.*?\.jpg'), etree.tostring(imagesList).decode("utf-8")) for imagesList in productTree.xpath('//*[@class="swiper-wrapper"]/img')])
print (imagesList)
输出:
[['http://www.example.com/static/p/no-name-8143-225244-1-product.jpg']]
注:我需要从网址中删除“-product”,我不知道为什么这个网址在两个方括号内。
你的解决方案是正确的,但我如何把它放在我的新的格式里,请看到新的Python代码。 – Jok3r
我没有完整的脚本,所以我很难帮助你。您可以尝试通过移除image.append API和嵌套方括号[]来查看它为您提供的内容,从而将该内容分解为两部分。就像是。 对于productList中的imagesList('// * [@ class =“swiper-wrapper”]/img') –
如果它给你你发布的字符串,那么注入我的代码来处理字符串,然后将处理后的url附加到imageList中。 –