@[TOC](Python 去掉字符串中的超链接href,email的两种方法(re模块,bs模块))
一: re模块
def html_tag_rm(content: str):
dr = re.compile(r'<[^>]+>',re.S)
return dr.sub('', content)
# 去除url
def del_href(content):
s = re.sub(r'^(https:\S+)', ' ', content)
s = re.sub(r'[a-zA-Z]+://[^\s]*', '', s)
return s
# 去除Email
def del_email(content):
s = re.sub(r'^([\w]+\.*)([\w]+)\@[\w]+\.\w{3}(\.\w{2}|)', ' ', content)
return s
二、bs模块
BeautifulSoup4是爬虫必学的技能。BeautifulSoup最主要的功能是从网页抓取数据,Beautiful Soup自动将输入文档转换为Unicode编码,输出文档转换为utf-8编码。BeautifulSoup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,如果我们不安装它,则 Python 会使用 Python默认的解析器,lxml 解析器更加强大,速度更快,推荐使用lxml 解析器。
from bs4 import BeautifulSoup
file = open('983/1.htm&#