python信息标记

(20) 2024-04-08 10:01:01

三种标记形式
xml
json
yaml

>>> for link in soup.find_all('a')
SyntaxError: invalid syntax
>>> for link in soup.find_all('a'):#查找所有标签a
	print(link.get('href'))

	
http://www.icourse163.org/course/BIT-268001
http://www.icourse163.org/course/BIT-1001870001

内容查找方法

Help on method find_all in module bs4.element:

find_all(name=None, attrs={}, recursive=True, text=None, limit=None, **kwargs) method of bs4.BeautifulSoup instance
    Extracts a list of Tag objects that match the given
    criteria.  You can specify the name of the Tag and any
    attributes you want the Tag to have.
    
    The value of a key-value pair in the 'attrs' map can be a
    string, a list of strings, a regular expression object, or a
    callable that takes a string and returns whether or not the
    string matches for some custom definition of 'matches'. The
    same is true of the tag name.

name:对标签名称的检索字符串。
attrs:对标签属性值的检索字符串,可标注属性检索。
recursive:是否对子孙全部检索,默认True
text:<>…</>中字符串区域的检索字符串。

>>> for tag in soup.find_all(True):#查找所有标签
	print(tag.name)

	
html
head
title
body
p
b
p
a
a
>>> import re
>>> for tag in soup.find_all(re.compile('b')):#查找所有以b开头的标签
	print(tag.name)

	
body
b
>>> soup.find_all('p','course')#p标签的course属性
>>> soup.find_all(id='link1')
[<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>]
>>> soup.find_all(id='link')
[]
>>> soup1.find_all(id='href')
[]
>>> soup.find_all(id=re.compile('link'))
[<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>, <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>]
>>> soup1.find_all(re.compile('page'))
[]

扩展方法

方法 说明
<>.find() 搜索且只返回一个结果,字符串类型,同.find_all()参数
<>.find_parents() 在先辈节点中搜索,返回列表类型,同.find_all()参数
<>.find_parent() 在先辈节点中返回一个结果,字符串类型,同.find()参数
<>.find_next_siblings() 在后续平行节点中搜索,返回列表类型,同.find_all()参数
<>.find_next_sibling() 在后续平行节点中返回一个结果,字符串类型,同.find()参数
<>.find_previous_siblings() 在前序平行节点中搜索,返回列表类型,同.find_all()参数
<>.find_previous_sibling() 在前序平行节点中返回一个结果,字符串类型,同.find()参数
>>> soup1.find_all(id=re.compile('next'))
[<a href="/page/2" id="next">下一頁</a>]
>>> soup1.find_all(string='高清')
['高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清']
>>> soup.find_all('a',recursive=False)
[]
>>> soup.find_all('a',recursive=True)
[<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>, <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>]
>>> soup1.find_all(text='高清')
['高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清']
>>> soup1.find_all(text=re.compile('清'))
['高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '清楚'高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清', '高清']
>>> for link in soup1.find_all('a',text='高清'):
SyntaxError: invalid character in identifier
>>> for link in soup1.find_all('a',text='高清'):
	print(link.get('href'))


>>> 
>>> soup1.find_all('a')

>>> soup1.find_all('a',string='高清')
[<a href="https://www.xxx/hd">高清</a>]
>>> soup1.find_all('a','button')
[]
>>> soup('a')
[<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>, <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>]
>>> 
THE END

发表回复