How to target data attribute with Scrapy

自作多情 提交于 2019-12-31 04:17:05

问题


I'm using Scrapy library to crawl a webpage.

But I have a problem. I do not know how to target data attribute.

I have an link with data attribute and href as follows:

<a data-item-name="detail-page-link" href="this-is-some-link">

What I want is the value of href. If a had class I could do it as follows:

response.css('.some-class::attr(href)') 

But the problem is that I do not know how to target data-item-name attribute.

Any advice?


回答1:


Using scrapy css selector, you can do :

response.css('a[data-item-name="detail-page-link"]::attr(href)').extract() 



回答2:


I'm not sure, if you can do this with the css method, but with the xpath method you should be able to do:

response.xpath("//a[@data-item-name]/@href")


来源:https://stackoverflow.com/questions/50734845/how-to-target-data-attribute-with-scrapy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!