怎么使用beautifulsoup获取指定div标签内容
7个回答
展开全部
一、使用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会节省数小时甚至数天的工作时间.
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会节省数小时甚至数天的工作时间.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
文档地址: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
应该可以满足你的需求
很久之前写过的部分代码
?
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
应该可以满足你的需求
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
怎么使用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下。
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下。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询