python获取指定标签中的内容
发布网友
发布时间:2022-04-22 04:06
我来回答
共1个回答
热心网友
时间:2023-10-20 19:07
你好:
请看下面代码:
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
html='''<dict>
<key>LogType</key>
<string>Default</string>
<key>Message</key>
<string>测试场景:订餐提交页面</string>
<string>Loop in : 2 rTime:0.266s</string>
<key>Timestamp</key>
<date>2014-06-06T12:16:24Z</date>
<key>Type</key>
<integer>1</integer>
</dict>'''
soup= BeautifulSoup(html)
trs=soup.findAll("string")
length=len(trs)
arr=[]
for i in range(length):
print trs[i].contents
需要安装BeautifulSoup,代码很容易懂的!
追问你好,我这个文件不是一个html文件,他是一个pilist文件,我用的方法是读出想要的内容但是都是unicode编码,我的脚本开头也有“#coding:utf-8”,请问如何处理呀?
追答ok!