作者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/cn.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