一、使用BeautifulSoup解析,能够得到一个 BeautifulSoup 的对象,并能按照标准的缩进格式的结构输出:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc)
print(soup.prettify())
#
#
#
# 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.
#
# ...
#
文档地址: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
应该可以满足你的需求
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="""
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