I need to select the first image I see (up the DOM) after the link. I have such a code, but for some reason it does not work, where can there be an error?
for doc in response.css('a::attr("href")'):
if '/mans/' in doc.get():
image = doc.xpath(f'//a[href={doc.get()}]/preceding::img[1]')
print(image)
could there be a syntax error? here is an example of the analyzed page...
<tbody>
<tr>
<td>
<img src="Resources/Images/Product images, logos/Alta, Alta HR/Alta_Black_Calendar_Horizontal_Shadow.png" alt="Fitbit Alta with the time shown on the screen" />
</td>
<td>
<p><a href="https://help.fitbit.com/manuals/manual_alta_de.pdf" target="_blank">Deutsch</a>
</p>
<p><a href="https://help.fitbit.com/manuals/manual_alta_en_US.pdf" target="_blank">English</a>
</p>
<p><a href="https://help.fitbit.com/manuals/manual_alta_es.pdf" target="_blank">Español</a>
</p>
</td>
<td>
<p><a href="https://help.fitbit.com/manuals/manual_alta_fr.pdf" target="_blank">Français</a>
</p>
<p><a href="https://help.fitbit.com/manuals/manual_alta_it.pdf" target="_blank">Italiano</a>
</p>
<p><a href="https://help.fitbit.com/manuals/manual_alta_ja.pdf" target="_blank">日本語</a>
</p>
</td>
<td>
<p><a href="https://help.fitbit.com/manuals/manual_alta_ko.pdf" target="_blank">한국어</a>
</p>
<p><a href="https://help.fitbit.com/manuals/manual_alta_zh_CN.pdf" target="_blank">简体中文</a>
</p>
<p><a href="https://help.fitbit.com/manuals/manual_alta_zh_TW.pdf" target="_blank">繁體中文</a>
</p>
</td>
</tr>
</tbody>
在 xpath 语句中的 href 前面放一个 @ 并且它应该可以工作,因为链接之前的元素是图像。
f'//a[@href={doc.get()}]/preceding::img[1]'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.