3种信息标记
- XML
- JSON
- YAML
1. XML
2. JSON
有类型的键值对
3. YAML
无类型键值对
信息查找
soup.find_all(name,attrs,recursive,string,**kwargs)
Python 3.7.2 (v3.7.2:9a3ffc0492, Dec 24 2018, 02:59:38)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license()" for more information.
>>> import requests
>>> r=requests.get("https://python123.io/ws/demo.html")
>>> r.status_code
200
>>> demo=r.text
>>> demo
'<html><head><title>This is a python demo page</title></head>\r\n<body>\r\n<p class="title"><b>The demo python introduces several python courses.</b></p>\r\n<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n<a href="http://www.icourse163.org/course/BIT-268001" class="py1" id="link1">Basic Python</a> and <a href="http://www.icourse163.org/course/BIT-1001870001" class="py2" id="link2">Advanced Python</a>.</p>\r\n</body></html>'
>>> soup.find_all('a')
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
soup.find_all('a')
NameError: name 'soup' is not defined
>>> from bs4 import BeautifulSoup
>>> soup.find_all('a')
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
soup.find_all('a')
NameError: name 'soup' is not defined
>>> soup=BeautifulSoup(demo,'html.parser')
>>> soup.find_all('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>]
>>> doup.find_all(['a','b'])
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
doup.find_all(['a','b'])
NameError: name 'doup' is not defined
>>> soup.find_all(['a','b'])
[<b>The demo python introduces several python courses.</b>, <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>]
>>> 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')):
print(tag.name)
body
b
>>> ## 返回了以b开头的标签
>>> print(soup.prettify())
<html>
<head>
<title>
This is a python demo page
</title>
</head>
<body>
<p class="title">
<b>
The demo python introduces several python courses.
</b>
</p>
<p class="course">
Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">
Basic Python
</a>
and
<a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">
Advanced Python
</a>
.
</p>
</body>
</html>
>>> soup.find_all('p','course')
[<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a> and <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p>]
>>> 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')
[]
>>> soup.a
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
>>> soup.a.href
>>> soup.a.'href'
SyntaxError: invalid syntax
>>> import re
>>> 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>]
>>> soup.a.sttrs['href']
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
soup.a.sttrs['href']
TypeError: 'NoneType' object is not subscriptable
>>> soup.a
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
>>> soup.a.attrs
{'href': 'http://www.icourse163.org/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}
>>> soup.a.sttrs['href']
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
soup.a.sttrs['href']
TypeError: 'NoneType' object is not subscriptable
>>> soup.a.attrs['href']
'http://www.icourse163.org/course/BIT-268001'
>>> soup.find_all('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>]
>>> soup.find_all('a',recursive=False)
[]
>>> soup.find_all(string = 'Basic Pyhton')
[]
>>> soup.find_all(string = 'Basic pyhton')
[]
>>> soup.find_all(string = 'Basic Python')
['Basic Python']
>>> soup.find_all(string = re.compile('pyhton'))
[]
>>> soup.find_all(string = re.compile('Pyhton'))
[]
>>> soup.find_all(string = re.compile('Python'))
['Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n', 'Basic Python', 'Advanced Python']
>>>