 
                        我在学习爬虫的时候想把里面的标签去掉,但是使用re.sub不成功。代码如下:
#!usr/bin/env python3
#coding:utf-8
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
html = urlopen("http://www.jianshu.com")
bsObj = BeautifulSoup(html,"lxml")
txt = bsObj.findAll("p",{"class":re.compile("abstract")})
for word in txt:
        if '<p class="abstract">' in word:
                newword = re.sub(r'<p class="abstract">','\n',word)
                print(newword)
        else:
                print(word)
请问这是是什么问题,谢谢!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
找到原因了,word不是一个字符串。
字符串对象p并不包含"sub"方法:
写一小段代码给题主参考一下子: