0
使用Python庫Scrapy,我做了以下內容:如何從此XPath獲取鏈接文本?
scrapy shell "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/"
從那裏我想獲得返回的每個項目的各個環節+文字:
response.xpath('//div[@class="title-and-desc"]/a')
然而,只有聯繫正在返回而不是文本。下面是所返回什麼樣:
response.xpath('//div[@class="title-and-desc"]/a')
[<Selector xpath='//div[@class="title-and-desc"]/a' data=u'<a target="_blank" href="http://www.brpr'>, <Selector xpath='//div[@class="title-and-desc"]/a' data=u'<a target="_blank" href="http://www.dive'>, <Selector xpath='//div[@class="title-and-desc"]/a' data=u'<a target="_blank" href="http://rhodesmi'>,
我可以通過上面的結果,其中i
是每個迭代變量循環:
i.xpath("text()").extract_first(),
i.xpath("@href").extract_first()
但只返回了@href
值。這是因爲text()
沒有任何結果檢索結果。什麼需要改變,所以我也可以得到相應的鏈接文本?
作爲參考,完整的Scrapy示例來自這裏:Scrapy Tutorial Example。
因爲只獲得文本意味着沒有得到的URL,這並不解決問題。我確實嘗試過'i.xpath(「// text()」)。extract_first()',但那不起作用。 – 4thSpace
@ 4thSpace它可以工作,請參閱我的編輯示例。 – Granitosaurus
另一個有用的選擇是在鏈接中使用XPath的'string()'或'normalize-space()':':print(l.xpath('normalize-space(。)')。extract_first(),l。 XPath的( '@ href' 屬性)。extract_first())' –