js问题:怎么删除指定内容的a标签
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js问题:怎么删除指定内容的a标签相关的知识,希望对你有一定的参考价值。
假设页面中有多个a标签类似<a href='' >aaa</a>,能否删除掉标签内容为aaa的标签?
window.onload = function()var as = document.getElementsByTagName("a");
for(var i = 0; i < as.length; i++)
var txt = as[i].innerText || as[i].textContent;
if(txt === "aaa")
as[i].parentNode.removeChild(as[i]);
;
jQuery
$(function()$("a").each(function()
$(this).text() === "aaa" && $(this).remove();
);
); 参考技术A 用jquery吧
怎么使用beautifulsoup获取指定div标签内容
参考技术A 一、使用BeautifulSoup解析,能够得到一个 BeautifulSoup 的对象,并能按照标准的缩进格式的结构输出:from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc)
print(soup.prettify())
# <html>
# <head>
# <title>
# The Dormouse's story
# </title>
# </head>
# <body>
# <p class="title">
# <b>
# The Dormouse's story
# </b>
# </p>
# <p class="story">
# Once upon a time there were three little sisters; and their names were
# <a class="sister" href="http://example.com/elsie" id="link1">
# Elsie
# </a>
# ,
# <a class="sister" href="http://example.com/lacie" id="link2">
# Lacie
# </a>
# and
# <a class="sister" href="http://example.com/tillie" id="link2">
# Tillie
# </a>
# ; and they lived at the bottom of a well.
# </p>
# <p class="story">
# ...
# </p>
# </body>
# </html>
二、简介:
Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会节省数小时甚至数天的工作时间. 参考技术B 文档地址:http://www.crummy.com/software/BeautifulSoup/bs4/doc/
很久之前写过的部分代码
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
f = urllib2.urlopen(url)
req = f.read()
soup = BeautifulSoup(req)
content = soup.findAll(attrs="name":"readonlycounter2")
subId = content[0].string.split(',')[1]
subName = soup.html.body.h1.span.string
content = soup.findAll(attrs="class":"subdes_td")
subType = content[0].string
subLeg = content[1].string
content = soup.findAll(attrs="colspan":"3")
subTime = content[2].string
subFile = content[7].div.string
应该可以满足你的需求 参考技术C f = urllib2.urlopen(url)
req = f.read()
soup = BeautifulSoup(req)
content = soup.findAll(attrs="name":"readonlycounter2")
subId = content[0].string.split(',')[1]
subName = soup.html.body.h1.span.string
content = soup.findAll(attrs="class":"subdes_td")
subType = content[0].string
subLeg = content[1].string
content = soup.findAll(attrs="colspan":"3")
subTime = content[2].string
subFile = content[7].div.string 参考技术D 怎么使用beautifulsoup获取指定div标签内容,例如
html="""
<html>
<body>
<div class="a">....</div>
<div class="b">i like it</div>
</body>
</html>
----------------------------
然后代码(大概)
from bs4 import BeautifulSoup
soup = BeautifulSoup(html);
content=soup.html.body.div ?? #这怎么用存在的class属性直接指定到该div下。 第5个回答 2016-02-07 f = urllib2.urlopen(url)
req = f.read()
soup = BeautifulSoup(req)
content = soup.findAll(attrs="name":"readonlycounter2")
subId = content[0].string.split(',')[1]
subName = soup.html.body.h1.span.string
content = soup.findAll(attrs="class":"subdes_td")
subType = content[0].string
subLeg = content[1].string
content = soup.findAll(attrs="colspan":"3")
subTime = content[2].string
subFile = content[7].div.string
以上是关于js问题:怎么删除指定内容的a标签的主要内容,如果未能解决你的问题,请参考以下文章