import html
import etree
selector = etree.HTML(res)
url_h2 = selector.xpath("//a[@_stat='video:poster_v']/../h2")[0]
url_div = selector.xpath("//a[@_stat='video:poster_v']/../div")[0]
url_h2 = etree.tostring(url_h2).decode()
url_div = etree.tostring(url_div).decode()
print("******", url, url_img, html.unescape(url_h2), html.unescape(url_div))
本文分享了使用Python中的etree模块与html模块解析HTML文本并进行解码的方法。通过XPath定位特定元素,并利用etree.tostring及html.unescape转换为可读字符串,实现了对网页数据的有效抓取。
840

被折叠的 条评论
为什么被折叠?



