什么是正確的語法:
//footer//a | (//a[not(//footer)] and position() <=200)
僅使用 //footer 如果存在,如果不存在,則查找所有 //a 不在 //footer 中并將其限制為 200
uj5u.com熱心網友回復:
你真的很親近。OR 運算子已經處理了您的情況 - 如果頁腳<a>下方不包含任何節點,則將捕獲第二個 OR 陳述句:
使用python和parsel(scrapy 的 html 決議器)。
>>> foo = Selector("<footer><a>text</a></footer>")
>>> bar = Selector("<div><a>text</a><a>text2</a><a>text3</a><a>text4</a></div>")
>>> foo.xpath("//footer//a | //a[position()<=2]").get()
'<a>text</a>'
>>> bar.xpath("//footer//a | //a[position()<=2]").extract()
['<a>text</a>', '<a>text2</a>']
注意:為了簡潔起見,我使用了2代替200。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/349830.html
