parik
parik

Reputation: 2415

How get href with a special src in child div in Python Scrapy

For getting all images of a site I write this code:

content = Selector(text = html)
all_images= content.css('img')
i = 0

for image in all_images:
    src =  image.css("::attr('src')").extract_first()

After getting the src of image, now I want to have the href of each image

<a href="/rayons/images" onclick="ga('send', 'event', 'computer HP', 'htwe', 'ope_xxl_s22Englos');">
    <img src="/mySrc/" alt="something" class="ze-content">
</a>

How can I get href when I know the Src ?

Upvotes: 1

Views: 469

Answers (1)

Valdir Stumm Junior
Valdir Stumm Junior

Reputation: 4667

AFAIK, you can't do parent searches using CSS. In this case, XPath is a better fit. You could do this:

for image in all_images:
    src =  image.css("::attr('src')").extract_first()
    href = image.xpath('parent::a/@href').extract_first()

Or, using XPath's abbreviated syntax:

href = image.xpath('../@href').extract_first()

Upvotes: 3

Related Questions