Найти - Пользователи
Полная версия: вопрос по xpath
Начало » Python для новичков » вопрос по xpath
1
gelius
Собираю ссылки залинкованные с картинок:
urls = lxml.html.fromstring(s)
urls = urls.xpath('//img/parent::a[not(re:match(@href, "(ban)")) ]/@href', namespaces={"re": "http://exslt.org/regular-expressions"})
Бывает вот так:
         <a href="/url.html" title="title" target="_blank">
                            <span class="class">
                                <img src="kartinka.jpg" alt="alt"/>
и такие ссылки этим кодом не собираются, благодря <span class=“class”>
Реально решить проблему оставив возможность собирать ссылки формата:
         <a href="/url.html" title="title" target="_blank">
                                <img src="kartinka.jpg" alt="alt"/>
john123
Попробуйте заместо оси parent использовать ancestor:
//img/ancestor::a[not(re:match(@href, "(ban)"))][1]/@href
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB