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
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16