作者bchoice (choice)
看板Python
標題[問題] 字串處理
時間Sun Apr 18 21:19:14 2021
各位大哥,小弟的程式碼有三行,請問有甚麼方式能簡化呢
text01 = item.find('p').find_next_sibling()
text02 = str(text01)
text03 = text02[4:-4]
print(text03)
謝謝各位
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 180.176.65.167 (臺灣)
※ 文章網址: https://webptt.com/m.aspx?n=bbs/Python/M.1618751956.A.CA7.html
1F:推 TitanEric: text01應該是element 所以也許有text01.string可以直 04/18 21:55
2F:→ TitanEric: 接拿裡面內容 04/18 21:55
element截取出來的前後會有<P>包住,請問要怎麼直接拿裡面的內容呢?
※ 編輯: bchoice (180.176.65.167 臺灣), 04/18/2021 22:02:15
3F:→ Hsins: 如果你用的是 lxml 這類 parser 可以找到元素之後 .text() 04/18 22:15
4F:→ Hsins: 這樣會拿到去掉 HTML ELEMENT 的內容 04/18 22:15
5F:推 fiorentino: text01 = str(item.find('p').find_next_sibling())[4 04/19 01:23
6F:→ fiorentino: :-4] 04/19 01:23
7F:推 legov: 謝謝!我再試試 04/19 08:27
8F:推 supremebboy: .find(text=True) 就可以了 04/20 01:34