Python3正则去掉HTML标签
Python3正则去掉HTML标签
1.引用一段代码
import re
html = '<pre class="line mt-10 q-content" accuse="qContent">\
目的是通过第一次soup.find按class粗略筛选并通过soup.find_all筛选出列表中的a标签并读入href和title属性<br><br>\
但是由于目标链接可能有图片链接,而这是我不想要的.请问如何去除?<br></pre>'
reg = re.compile('<[^>]*>')
print(reg.sub('',html))
2.重点
reg = re.compile('<[^>]*>')
print(reg.sub('',html))
3.实例
开始
import requests
import re
from bs4 import BeautifulSoup
retxt=open('test.log','r')
for x in range(250,999):
#rurl=rurl.strip('\n')
url='http://ananas.mooc1.mti100.com/tologin?fid={0}'.format(x)
#print(url)
try:
response=requests.get(url,timeout=1).text
#print(response)
soup=BeautifulSoup(response,features="lxml")
result=soup.find_all('span',attrs={'class':'l_schoolName2'})
print('学校:{0}'.format(result))
except requests.exceptions.InvalidURL:
pass
except requests.exceptions.ConnectionError:
pass
except requests.exceptions.ReadTimeout:
pass
输出
学校:[<span class="l_schoolName2" id="schoolName2">
杭州师范大学
</span>]
学校:[<span class="l_schoolName2" id="schoolName2">
去除标签之后
import requests
import re
from bs4 import BeautifulSoup
#retxt=open('test.log','r')
for x in range(250,999):
#rurl=rurl.strip('\n')
url='http://ananas.mooc1.mti100.com/tologin?fid={0}'.format(x)
#print(url)
try:
response=requests.get(url,timeout=1).text
#print(response)
soup=BeautifulSoup(response,features="lxml")
result=soup.find_all('span',attrs={'class':'l_schoolName2'})
reg=re.compile('<[^>]*>',re.S)
print('学校:{0}'.format(reg.sub('',str(result))))
except requests.exceptions.InvalidURL:
pass
except requests.exceptions.ConnectionError:
pass
except requests.exceptions.ReadTimeout:
pass
输出
学校:[]
学校:[]
学校:[
上海电子信息职业技术学院
]
学校:[]
学校:[
超星大学
Python3正则去掉HTML标签的更多相关文章
- 正则去掉html标签之间的空格、换行符、tab符,但是保留html标签内部的属性空格
今天遇到一个比较少见的去空格: 正则去掉html标签之间的空格.换行符.tab符,但是保留html标签内部的属性空格 JS 举例: "<a href='baidu.com' name= ...
- 正则去掉img标签的style样式
$body = '<div style="width:100px; height:20px;"><img alt="test" src=&qu ...
- 正则去掉html标签
return System.Text.RegularExpressions.Regex.Replace(HTMLStr, "<[^>]*>", "&qu ...
- 正则去掉 html标签
var htmlStr='<p class="cjk" style="margin-bottom: 0cm; line-height: 16px;"> ...
- python去掉html标签
s = '<SPAN style="FONT- SIZE: 9pt">开始1~3<SPAN lang=EN-US>& lt;?xml:namespa ...
- Python:使用正则去除HTML标签(转)
利用正则式处理,不知道会不会有性能问题,没有经过太多测试. 目前我有很多还是使用BeautifulSoup进行这种处理. HTML实体处理的只是用于处理一些常用的实体. # -*- coding: u ...
- js去掉html标签和去掉字符串文本的所有的空格
去掉html标签的js <script> function delHtmlTag(str){ return str.replace(/<[^>]+>/g,"& ...
- java去掉jsp标签内容的方法
//去掉内容的标签 public static String removeTag(String count){ try { int tagCheck=-1; do { i ...
- css去掉a标签点击后的虚线框,outline,this.blur()
css去掉a标签点击后的虚线框,outline,this.blur() outline是css3的一个属性,用的很少.声明,这是个不能兼容的css属性,在ie6.ie7.遨游浏览器都不兼容. outl ...
随机推荐
- 通过示例学习rholang(上部:课程0-6)
通过例子和实践来学习rho语言.下面的例子和练习都很值得去运行.阅读.修改和完善.修改练习和教程中任何你感到有意思的代码,这样能够获得最好的学习效果.该教程包含了rho语言最常见以及最重要的特性,足以 ...
- python读取txt打印(print)出乱码的问题
如下图所示,print第一行首位出现乱码的问题 网上的解答是因为UTF-8的BOM前缀(\xef\xbb\xbf) 解决这个问题的方法很多,最快捷的方法是txt文本另存为的时候更改编码格式 将txt另 ...
- django 启动错误:Generator expression must be parenthesized 错误信息:
错误为: Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x ...
- Map梳理
Map梳理 类型介绍 通用Map:用于在应用程序中管理映射,通常在 java.util 程序包中实现 HashMap.Hashtable.Properties.LinkedHashMap.Identi ...
- CTF--HTTP服务--路径遍历(提升root用户权限)
开门见山 1. 在上次实验取的靶机低用户权限,查看该系统的内核版本 2. 查看该系统的发行版本 3. 查看该内核版本是否存在内核溢出漏洞,并没有 如果有内核溢出漏洞,则可以 4. 在靶机查看/etc/ ...
- 乌班图14更新软件提示错误:https://mirrors.aliyun.com kubernetes-xenial InRelease: 由于没有公钥,无法验证下列签名: NO_PUBKEY 6A030B21BA07F4FB
提示如下 获取: https://mirrors.aliyun.com kubernetes-xenial InRelease 忽略 https://mirrors.aliyun.com kubern ...
- K8S提示Init:ImagePullBackOff
[root@master opt]# kubectl get pods -n kube-system NAME READY STATUS RESTARTS AGE coredns-9d85f5447- ...
- 1-NoSQL介绍及Redis安装
背景 随着互联网的不断发展和软件架构的不断复杂化,同时随着网站访问量的日渐上升,导致传统单机关系型数据库应用已经无法满足人们的需求,在高并发的场景下,频繁的数据库存取操作使得服务器压力剧增,甚至导致服 ...
- ceph问题
问题1: [root@admin-node my-cluster]# ceph -s cluster 4ca35731-2ccf-47fb-9f06-41fae858626d health HEALT ...
- 中国天气网API接口
http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data/cityinfo/101010100.h ...