+ '(.*?)',re.S)
msg = re.findall(patt_content,content)
for item in msg:
str = str + '\n作者-----' + item[0] + '\n' + '帖子内容-----' + item[1].strip() + '\n'
str = self.tool.replace(str)
# print u'作者',item[0],u'帖子内容:',item[1]
return str
#写文件
def writeStr2File(self,out_put_file,str1,append = 'a'):
# 去掉文件,保留路径。比如 'a/b/c/d.txt' 经过下面代码会变成 'a/b/c'
subPath = out_put_file[:out_put_file.rfind('/')]
# 如果给定的路径中,文件夹不存在,则创建
if not os.path.exists(subPath):
os.makedirs(subPath)
# 打开文件并将 str 内容写入给定的文件
with open(out_put_file, append) as f:
f.write(str1.strip()+'\n')
def start_crawl(self):
page_num = self.get_page_num()
if page_num == None:
print "url已失效,请重试!"
return
print u"该帖子共有" + str(page_num) + u"页数据"
for i in range(1,int(page_num)+1):
print u"正在写入第" + str(i) + u"页数据..."
content = "正在写入第" + str(i) + u"页数据------------------------------------\n" + self.get_tiebar(i)
self.writeStr2File(self.out_put_file,content)
class Tool:
#去除img标签
patt_img = re.compile(r'
patt_herf = re.compile(r'
patt_br = re.compile(r'
{1,3}')
def replace(self,content):
content = re.sub(self.patt_img,"",content)
content = re.sub(self.patt_herf,"",content)
content = re.sub(self.patt_br,"\n",content)
return content.strip()
tiebar = Tiebar('/tupian/20230522/error.html title = tiebar.get_titile()
# page_num = tiebar.get_page_num()
# print title,page_num
tiebar.start_crawl()
“怎么用python抓取百度贴吧内容”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联-成都网站建设公司网站,小编将为大家输出更多高质量的实用文章!
文章名称:怎么用python抓取百度贴吧内容-创新互联
分享链接:http://www.cdkjz.cn/article/gciso.html