我們可以對元素中的文本使用哪些其他方法?
>>> products.css('h2.entry-title').get()
'<h2 itemprop="headline"><a href="https://example.com/index.php/2021/12/12/your-20-with-few-clicks-from-stash/" rel="bookmark">Your $20 With Few Clicks From Stash</a></h2>'
但試圖獲取文本,您的 20 美元只需從 Stash 中點擊幾下即可使用
products.css('h2.entry-title::text').get()
>>> products.css('h2.entry-title::text').get()
>>>
不管用。有什么建議嗎?謝謝你。
uj5u.com熱心網友回復:
實際上,所需的文本節點Your $20 With Few Clicks From Stash 在a tag. 要獲得正確的輸出,css 運算式如下:
products.css('h2.entry-title a::text').get().strip()
在scrapy shell中的實作:
In [6]: from scrapy.selector import Selector
In [7]: %paste
html_doc="""
<html>
<body>
<h2 itemprop="headline">
<a href="https://example.com/index.php/2021/12/12/your-20-with-few-clicks-from-stash/" rel="bookmark">
Your $20 With Few Clicks From Stash
</a>
</h2>
</body>
</html>
"""
## -- End pasted text --
In [8]: sel = Selector(text=html_doc)
In [9]: sel.css('h2.entry-title a::text').get().strip()
Out[9]: 'Your $20 With Few Clicks From Stash'
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/387385.html
