日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MacOS下安装BeautifulSoup库及使用

發布時間:2025/3/15 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MacOS下安装BeautifulSoup库及使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

BeautifulSoup簡介


BeautifulSoup庫是一個強大的python第三方庫,它可以解析html進行解析,并提取信息。

安裝BeautifulSoup


  • 打開終端,輸入命令:
pip3 install beautifulsoup4

BeautifulSoup庫小測


  • 小測用到的html頁面地址:http://python123.io/ws/demo.html

  • 查看它的源代碼:

  • 用request庫獲得源代碼(存放在變量demo中):
>>> import requests >>> r = requests.get("http://python123.io/ws/demo.html") >>> r.text '<html><head><title>This is a python demo page</title></head>\r\n<body>\r\n<p class="title"><b>The demo python introduces several python courses.</b></p>\r\n<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n<a href="http://www.icourse163.org/course/BIT-268001" class="py1" id="link1">Basic Python</a> and <a href="http://www.icourse163.org/course/BIT-1001870001" class="py2" id="link2">Advanced Python</a>.</p>\r\n</body></html>' >>> demo = r.text
  • 導入BeautifulSoup庫
>>> from bs4 import BeautifulSoup >>>
  • 使用BeautifulSoup庫解析html信息
>>> demo = r.text >>> soup = BeautifulSoup(demo,'html.parser') >>> print(soup.prettify) <bound method Tag.prettify of <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>> >>>

如何使用BeautifulSoup庫?

  • 代碼框架:
from bs4 import BeautifulSoup soup = BeautifulSoup('<p>data</p>','html.parser')
  • 其中BeautifulSoup的兩個參數:
    • 第一個代表我們要解析的html格式的信息。
    • 第二個代表解析所使用到的解析器

轉載于:https://www.cnblogs.com/031602523liu/p/9824907.html

總結

以上是生活随笔為你收集整理的MacOS下安装BeautifulSoup库及使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。