码迷,mamicode.com
首页 > 其他好文 > 详细

BeautifulSoup

时间:2020-02-26 01:09:13      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:rip   python   The   code   work   http   rpo   bsp   ram   

 

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

 

BeautifulSoup

标签:rip   python   The   code   work   http   rpo   bsp   ram   

原文地址:https://www.cnblogs.com/leisurelyRD/p/12364834.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!