代码:
import requests
from bs4 import BeautifulSoup
r = requests.get("https://python123.io/ws/demo.html")
demo = r.text
soup = BeautifulSoup(demo, "html.parser")
print(soup.body)
print(type(soup.a.parents))
print('\n')
for parent in soup.a.parents:
if parent is None:
print('1')
print(parent.name)
else:
print('2')
print(parent.name)
结果:
D:\python_install\python.exe D:/pycharmworkspace/temp1/crawler_1.py
<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>
<class 'generator'>
2
p
2
body
2
html
2
[document]
Process finished with exit code 0
OK