BeatutifulSoup基础

soup本身:

111222


可以看到,1.soup.find和soup.find_all得到的结果包含查找标签本身;2.find的结果不是列表,find_all的结果是列表
>>> soup.find('p')

111222

>>> soup.find_all('p') [

111222

]


接下来讨论子列的情况:

contents是将子列生成列表

soup.contents是将所有内容成为列表的一个元素

>>> soup.contents
[

111222

]

soup.tag.contents,1.去除了本身的标签;2.平行子列标签成为列表的各个元素,但只生成一个列表

soup.find('p').contents
[111, 222]


children和descendents都只能通过遍历得到,并且都不是列表

soup.children 得到所有内容,并且不是列表

>>> for i in soup.children:
	print(i)	

111222

soup.tag.children 得到标签下的平行子列标签,也不是列表

>>> for i in soup.find('p').children:
	print(i)	
111
222

ps:

soup=BeautifulSoup('

','html.parser')

find_all查找到的所有tag组成列表,所以假如有平行的,就会有多个元素了

>>> soup.find_all('c')
[, ]


一个tag find_all自己会生成空列表
>>> soup.p.find_all('p')
[]

contents本身生成列表,很好理解

>>> soup.find('p').contents
[, ]

这是会生成多个列表的情况,先用children遍历出平行xml,但这些xml不组成列表,在对xml用find_all就会生成,各自的列表.

>>> for c in soup.find('p').children:
	print(c.find_all('a'))

	
[]
[]











你可能感兴趣的:(python)