python信息标记

三种标记形式
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>]
>>> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值