チュートリアル
リクエスト ライブラリは、HTML ページを解析するために美しいスープ ライブラリと組み合わせて使用されます。
インストールコマンドpip install beautifulsoup4
ユニット 4: 美しいスープ ライブラリの使用を開始する
beautifulsoup4ライブラリのインストール
デモ hmtl ページのアドレス: http://python123.io/ws/demo.html
ソースコードを特定する
import requests
r = requests.get('http://python123.io/ws/demo.html')
print(r.text)
解析された HTML ページのテキスト コンテンツ
<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 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>
</body></html>
通常の HTML ページに解析される
import requests
r = requests.get('http://python123.io/ws/demo.html')
demo = r.text
from bs4 import BeautifulSoup #重点
soup = BeautifulSoup(demo,"html.parser")#重点
解析されたルールページ
<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>
Beautiful Soup ライブラリの基本要素
HTMLとXMLを解析するための関数ライブラリです(タグの解析とトラバーサルテキスト「タグツリー」の維持)
最も一般的に使用されるインポート方法:from bs4 import BeautifulSoup
使い方:soup = BeautifulSoup("<html>data</html>","html.parser")
soup2 = BeautifulSoup(open("D://demo.hmtl"),"html.parser")
。
美しいスープパーサー
Beautiful Soup クラスの基本要素
ラベルタグ:
ラベルの名前 名前:
ラベルの属性
ラベル内の非属性文字列 NavigableString (複数のラベル レベルにまたがることができます)
タグ内の文字列のコメント位置 Comment
bs4ライブラリに基づくHTMLコンテンツトラバーサル手法
並列トラバーサルは同じ親ノードの下で発生します
bs4ライブラリをベースにしたHTML形式出力
bs ライブラリの prettify() メソッドにより、HTML ページがより使いやすくなります
ユニット 5: 情報の構成と抽出方法
情報ラベルの 3 つの形式
3 つの形式の情報マーキングの比較
情報抽出への一般的なアプローチ
**例:** Web サイトのすべての Web ページを検索します。
- すべての a タグをクエリする
- タグの後のhref
bs4ライブラリをベースとしたHTMLコンテンツ検索方式
名前:
属性:
再帰的:
弦:
ユニット 6: 例 1: 中国の大学ランキング クローラー
事例紹介
http://www.zuihaodaxue.cn/zuihaodaxuepaiming2016.html
書き方例
#爬取中国大学排名前十
import requests
from bs4 import BeautifulSoup
import bs4
def getHTMLText(url):
try:
r = requests.get(url, timeout=30)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return ""
def fillUnivList(ulist, html):
soup = BeautifulSoup(html, "html.parser")
ss = soup.find_all('tbody')
for tr in soup.find('tbody').children:
if isinstance(tr, bs4.element.Tag):
tds = tr('td')
#大学在a标签当中
ulist.append([tds[0].string.strip(), tds[1].a.string.strip(), tds[2].contents[0].strip(),tds[3].contents[0].strip(),tds[4].string.strip()])
def printUnivList(ulist, num):
for i in range(num):
u = ulist[i]
print("{:<5}{:<12}{:<6}{:9}{:<9}".format(u[0],u[1],u[2],u[3],u[4]))
def main():
uinfo = []
print("{}\t{}\t\t\t{}\t{}\t\t\t\t{}".format("排名","大学名称","地点","类型","总分"))
url = 'https://www.shanghairanking.cn/rankings/bcur/2021'
html = getHTMLText(url)
fillUnivList(uinfo, html)
printUnivList(uinfo, 10) #排名前十的大学
main()