美文网首页
lxml中方法:tostring( )和fromstring()

lxml中方法:tostring( )和fromstring()

作者: 小飞船1号 | 来源:发表于2020-05-09 14:20 被阅读0次
推荐使用第二种

fromstring(tring):将 string 解析为 Element 或者 ElementTree 。

tostring( )方法:将一个 Element 或者 ElementTree 转换为 string 形式,但是结果为bytes类型,因此需要利用decode()方法将其转成str类型

eg1:

# 访问详情页

from lxmlimport html

def news_detail(url,xpathstr):

data=html.fromstring(toHtml(url))

c=data.xpath(xpathstr)[0]

c = html.tostring(c).decode()#   结果为bytes类型,需要利用decode()方法将其转成str类型

return c

相关文章

网友评论

      本文标题:lxml中方法:tostring( )和fromstring()

      本文链接:https://www.haomeiwen.com/subject/paovwhtx.html