怎么使用beautifulsoup获取指定div标签内容

2024-12-22 09:41:28
推荐回答(5个)
回答1:

一、使用BeautifulSoup解析,能够得到一个 BeautifulSoup 的对象,并能按照标准的缩进格式的结构输出:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc)

print(soup.prettify())
#
#
# <br># The Dormouse's story<br>#
#
#
#


#
# The Dormouse's story
#

#


#


# Once upon a time there were three little sisters; and their names were
#
# Elsie
#

# ,
#
# Lacie
#

# and
#
# Tillie
#

# ; and they lived at the bottom of a well.
#


#


# ...
#


#
#

二、简介:
Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会节省数小时甚至数天的工作时间.

回答2:

文档地址: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

应该可以满足你的需求

回答3:

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

回答4:

怎么使用beautifulsoup获取指定div标签内容,例如
html="""


....

i like it



----------------------------
然后代码(大概)
from bs4 import BeautifulSoup
soup = BeautifulSoup(html);
content=soup.html.body.div ?? #这怎么用存在的class属性直接指定到该div下。

回答5:

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