Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python Scrapy中的子div中使用特殊的src获取href_Python_Xpath_Css Selectors_Scrapy_Selector - Fatal编程技术网

如何在Python Scrapy中的子div中使用特殊的src获取href

如何在Python Scrapy中的子div中使用特殊的src获取href,python,xpath,css-selectors,scrapy,selector,Python,Xpath,Css Selectors,Scrapy,Selector,为了获取站点的所有图像,我编写了以下代码: content = Selector(text = html) all_images= content.css('img') i = 0 for image in all_images: src = image.css("::attr('src')").extract_first() 在获得图像的src之后,现在我想获得每个图像的href <a href="/rayons/images" onclick="ga('send', 'e

为了获取站点的所有图像,我编写了以下代码:

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

for image in all_images:
    src =  image.css("::attr('src')").extract_first()
在获得图像的src之后,现在我想获得每个图像的href

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


当我知道Src时,如何获得href

AFAIK,您不能使用CSS进行父级搜索。在本例中,XPath更适合。您可以这样做:

for image in all_images:
    src =  image.css("::attr('src')").extract_first()
    href = image.xpath('parent::a/@href').extract_first()
或者,使用XPath:

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