簡體   English   中英

如何使用ScraPY xPath以字符串形式獲取屬性值

[英]How do I get the attribute value as string with ScraPY xPath

Scrapy文檔說,要獲取元素的字符串表示形式,我必須執行“ string()”

所以我做了:

for url in response.xpath('string(//a[@class="thumbnail"]/@href)'):
        print(url)

如果不使用字符串,則只打印所有內容,而使用string()則只打印第一個元素的選擇器,然后關閉蜘蛛網。 我究竟做錯了什么?

response.xpath('string(//a[@class="thumbnail"]/@href)') #this is a selector object (list of selectors)

response.xpath('string(//a[@class="thumbnail"]/@href)').extract() # in your case href attribute (list of hrefs)


for url in response.xpath('string(//a[@class="thumbnail"]/@href)').extract():
        print(url) #actual href as found in html (can be relative or absolute)

docs 選擇器中的更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM