查看print的语法:
for index, link in enumerate(links):
args = (index, link.xpath('@href').extract(),
link.xpath('img/@src').extract())
print ('Link number %d points to url %s and image %s' % args)
此代码适用于python 3.6。它发送:
Link number 0 points to url [u'image1.html'] and image [u'image1_thumb.jpg']
但使用f“...... {var}”是否存在等价物?
看起来这个不起作用:
print (f'Link number {repr(enumerate)} points to url {str(index)} and image {str(link)}')
发送:
Link number 0 points to url <Selector xpath='//a[contains(@href, "image")]' data='<a href
="image1.html">Name: My image 1 <'>
您可以在此troubleshooting article tuto中找到完整代码。
答案 0 :(得分:3)
感谢@JonClements,以下是答案:
print('Link number {} points to url {} and image {}'.format(*args))