BeautifulSoup

from bs4 import BeautifulSoup
import requests

url = ‘https://python123.io/ws/demo.html‘
r = requests.get(url)
getHTML = r.text
soup = BeautifulSoup(getHTML,"html.parser")
print(soup.prettify())
D:\python_work\venv\Scripts\python.exe D:/python_work/test.py
<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>

Process finished with exit code 0
from bs4 import BeautifulSoup
import requests

url = ‘https://python123.io/ws/demo.html‘
r = requests.get(url)
getHTML = r.text
soup = BeautifulSoup(getHTML,"html.parser")

# 提取a标签的属性href的值
for link in soup.find_all(‘a‘):  
    print(link.get(‘href‘))


运行结果:
D:\python_work\venv\Scripts\python.exe D:/python_work/test.py
http://www.icourse163.org/course/BIT-268001
http://www.icourse163.org/course/BIT-1001870001

Process finished with exit code 0

相关推荐