python爬虫怎么可以只取div中的值不取后面的标签

问题:通过python爬虫,结果如图:

想要去掉红圈里的,只保留日期。

代码:

 pattern = re.compile('在线出版日期.*?<div class="info_right author">(.*?)</div>', re.S)
    online_date = pattern.findall(html)
    if online_date:
        online_date = online_date[0].strip()

网页:

解决方法:用xpath中的text()方法:

lx=etree.HTML(html)
online_date = lx.xpath('//ul[@class="info"]/li/div[@class="info_right author"]')[-2].text
发布了233 篇原创文章 · 获赞 20 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_42565135/article/details/104271920