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 ...
随机推荐
- 读取配置文件,appsettings.json和注入ICO
https://www.cnblogs.com/knowledgesea/p/7079880.html 引入Nuget的两个类库 Microsoft.Extensions.Configuration ...
- limit_choices_to
# 多对多 老师 teachers = models.ManyToManyField(verbose_name='任课老师', to='UserInfo', related_name="ab ...
- 13.系统总结static
static修饰属性和方法: package com.oop.demo08; //static :被static修饰的变量或者方法随类加载,从属于类,当然对象也可以调用 public class St ...
- 国产CPU 申威1621 异数OS基础组件理论性能测试报告
国产CPU 申威1621 异数OS基础组件理论性能测试报告 文章目录 国产CPU 申威1621 异数OS基础组件理论性能测试报告 前言 测试平台 测试项目 SW1621 异数OS 容器虚拟交换机模拟性 ...
- SpringMVC简单使用教程
一.SpringMVC简单入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 <!--conf ...
- Frameworks.Entity.Core 7
1描述:实体基类,与业务和架构无关名称:EntityBase属性:public abstract 2描述:/ MongoDB的一些扩展方法名称:MongoExtensions修饰: public st ...
- SpringBoot中对SpringMVC的自动配置
https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/htmlsingle/#boot-features-developin ...
- Git详解之安装
前言 是时候动手尝试下 Git 了,不过得先安装好它.有许多种安装方式,主要分为两种,一种是通过编译源代码来安装:另一种是使用为特定平台预编译好的安装包. 从源代码安装 若是条件允许,从源代码安装有很 ...
- .net core 认证与授权(一)
前言 .net core web并不是一个非常新的架构,很多文章提及到认证与授权这个过程,但是一般都会提及到里面的方法怎么用的,而不是模拟一个怎样的过程,所以我打算记录自己的理解. 什么是认证?我们大 ...
- idea maven 动态打包指定环境
jar pom.xml <!-- 指定文件id --> <profiles> <profile> <id>alpha</id> <pr ...