xpath中使用正則表達式
其實我自己也從來沒用到過,在此記錄一下,萬一以后會用到呢。
比如有個網站正文部分是: //*[@id='postmessage_32199']
另一個同級別頁面的正文是: //*[@id='postmessage_32153']
要抓取這種正文其實可以用xpath: //*[starts-with(@id, 'postmessage_')]
或者 //*[contains(@id, 'postmessage_')]
也可以選擇在xpath中使用正則表達式:doc.xpath(r'//*[re:match(@id, "postmessage_\d+")]', namespace={"re": "http://exslt.org/regular-expressions"})
xpath中如何看選住原文的內容
選取頁面元素el,通過to_string 方法可以拿到頁面標簽的原文不過是bytes類型,可以用bytes.decoding result = etree.tostring(el) print(result.decode('utf-8'))