BeautifulSoup

Posted leisurelyrd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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_workvenvScriptspython.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_workvenvScriptspython.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的主要内容,如果未能解决你的问题,请参考以下文章

Python练习册 第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-),(http://tieba.baidu.com/p/2166231880)(代码片段

python 爬虫 requests+BeautifulSoup 爬取简单网页代码示例

python3 requests + BeautifulSoup 爬取阳光网投诉贴详情实例代码

使用 Python 和 BeautifulSoup(将网页源代码保存到本地文件中)

使用 selenium 和 beautifulsoup 的慢代码

爬虫BeautifulSoup库基本使用,案例解析(附源代码)