目錄
1 BeautifulSoup 官方文檔
2 用bs 和 requests 打開(kāi) 本地html的區(qū)別:代碼里的一段html內(nèi)容
2.1 代碼和運(yùn)行結(jié)果
2.2 用beautiful 打開(kāi) 本地 html 文件
2.2.1 本地html文件
2.2.2?soup1=BeautifulSoup(html1,"lxml")
2.3?用requests打開(kāi) 本地 html 文件
2.3.1 本地html文件
2.3.2 print(html1)
3 用bs 和 requests 打開(kāi) 本地html的區(qū)別:一個(gè)獨(dú)立的html文件
3.1 獨(dú)立創(chuàng)建一個(gè)html文件
3.2 下面是新得代碼和運(yùn)行結(jié)果
3.3 用beautiful 打開(kāi) 本地 html 文件
3.3.1 語(yǔ)法差別??soup1=BeautifulSoup(open(path1))
3.4 用 read() 打開(kāi) 本地 html 文件
3.4.1 語(yǔ)法差別?with open(path1 ,"r") as f:? ?和? res=f.read()
3.5 用requests打開(kāi) 本地 html 文件
4? f.write(soup1.prettify()) 和 html 用 read()讀出來(lái)?差別很大
1 BeautifulSoup 官方文檔
Beautiful Soup: We called him Tortoise because he taught us.https://www.crummy.com/software/BeautifulSoup/
Beautiful Soup 4.4.0 文檔 — Beautiful Soup 4.2.0 中文 文檔https://beautifulsoup.readthedocs.io/zh_CN/v4.4.0/
Beautiful Soup 4.4.0 文檔 — beautifulsoup 4.4.0q 文檔https://beautifulsoup.readthedocs.io/zh_CN/latest/
?
2 用bs 和 requests 打開(kāi) 本地html的區(qū)別:代碼里的一段html內(nèi)容
2.1 代碼和運(yùn)行結(jié)果
#E:\work\FangCloudV2\personal_space\2learn\python3\py0003.txt
import requests
from bs4 import BeautifulSoup
#html文件內(nèi)容
html1 = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a class="sister" id="link1">Elsie</a>,
<a class="sister" id="link2">Lacie</a> and
<a class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
#"測(cè)試bs4"
print ("測(cè)試bs4")
soup1=BeautifulSoup(html1,"lxml")
print (soup1.prettify())
#"對(duì)比測(cè)試requests"
print ("對(duì)比測(cè)試requests")
#res=requests.get(html1)
res=html1
#print (res.text)
print (res)
?
2.2 用beautiful 打開(kāi) 本地 html 文件
#"測(cè)試bs4"
html1="""? ... """
print ("測(cè)試bs4")
soup1=BeautifulSoup(html1,"lxml")
print (soup1.prettify())
2.2.1 本地html文件
- 這次的本地html 文件是寫(xiě)在 python 腳本內(nèi)容一起的 一段文本
- html1=""" ...? """
2.2.2?soup1=BeautifulSoup(html1,"lxml")
- 正確寫(xiě)法
- soup1=BeautifulSoup(html1,"lxml")
- lxml 是解析方式
- 如果不寫(xiě),默認(rèn)也會(huì)采用 lxml的解析
- 如果寫(xiě)成 soup1=BeautifulSoup(html1) 可以正常運(yùn)行,但是會(huì)提醒
lxml
html.parser
應(yīng)該這幾種都可以
2.3?用requests打開(kāi) 本地 html 文件
#"對(duì)比測(cè)試requests"
print ("對(duì)比測(cè)試requests")
#res=requests.get(html1)
res=html1
#print (res.text)
print (res)
2.3.1 本地html文件
- 這次的本地html 文件是寫(xiě)在 python 腳本內(nèi)容一起的 一段文本
- html1=""" ...? """
- 本地文件 html 已經(jīng)是一段 腳本內(nèi)的文本? """? ..."""
2.3.2 print(html1)
本地文件 html 已經(jīng)是一段 腳本內(nèi)的文本? """? ..."""
- 正確寫(xiě)法1?
- res=html1
- print (res)
- 正確寫(xiě)法2
- print (html1)
- 錯(cuò)誤寫(xiě)法1
- #print (res.text)
- #print (html1.text)
- 只有html作為網(wǎng)頁(yè)結(jié)構(gòu)的時(shí)候,可以用? html.text 取到其中的string? 內(nèi)容
- 所以?
- requests.get(url)?
- requests.get(url).text
requests.exceptions.InvalidSchema: No connection adapters were found for '<html><head><title>The Dormouse\'s story</title></head>\n<body>\n<p class="title"><b>The Dormouse\'s story</b></p>\n\n<p class="story">Once upon a time there were three little sisters; and their names were\n<a class="sister" id="link1">Elsie</a>,\n<a class="sister" id="link2">Lacie</a> and\n<a class="sister" id="link3">Tillie</a>;\nand they lived at the bottom of a well.</p>\n\n<p class="story">...</p>\n'
- 錯(cuò)誤寫(xiě)法2
- #res=requests.get(html1)
- 一樣的原因
- 因?yàn)檫@里的html1 不是網(wǎng)頁(yè),而已經(jīng)是網(wǎng)頁(yè)的內(nèi)容string了!
AttributeError: 'str' object has no attribute 'text'
3 用bs 和 requests 打開(kāi) 本地html的區(qū)別:一個(gè)獨(dú)立的html文件
3.1 獨(dú)立創(chuàng)建一個(gè)html文件
3.2 下面是新得代碼和運(yùn)行結(jié)果
代碼
#E:\work\FangCloudV2\personal_space\2learn\python3\py0003-1.txt
#E:\work\FangCloudV2\personal_space\2learn\python3\html0003.html
import requests
import os
import time
from bs4 import BeautifulSoup
path1=r"E:\work\FangCloudV2\personal_space\2learn\python3\html0003.html"
soup1=BeautifulSoup(open(path1))
print ("測(cè)試bs4")
print (soup1.prettify())
path2=r'E:\work\FangCloudV2\personal_space\2learn\python3\html0003-1.html'
if not os.path.exists(path2):
os.mkdir(path2)
with open(path2 ,"a") as f:
f.write("測(cè)試bs4")
f.write(soup1.prettify())
print ("對(duì)比測(cè)試requests")
with open(path1 ,"r") as f:
res=f.read()
print (res)
with open(path2 ,"a") as f:
f.write("對(duì)比測(cè)試requests")
f.write(res)
"""
#地址,路徑,前都記得加 r, 因?yàn)閟tring 內(nèi)部包含\/等轉(zhuǎn)義符,rawdata安全
url1="E:\work\FangCloudV2\personal_space\2learn\python3\html0003.html"
url1=r"E:\work\FangCloudV2\personal_space\2learn\python3\html0003.html"
res=requests.get(url1)
#本地地址不能像網(wǎng)址 url這樣用,用的\/不同,即使用 raw r 也不行. 可以用轉(zhuǎn)格式函數(shù)嗎?
#https://www.baidu.com/
"""
運(yùn)行結(jié)果
另存為的文件內(nèi)容
3.3 用beautiful 打開(kāi) 本地 html 文件
3.3.1 語(yǔ)法差別??soup1=BeautifulSoup(open(path1))
最大的差別
- soup1=BeautifulSoup(open(path1))
- soup1.prettify() 輸出格式化得內(nèi)容
path1=r"E:\work\FangCloudV2\personal_space\2learn\python3\html0003.html"
soup1=BeautifulSoup(open(path1))
print ("測(cè)試bs4")
print (soup1.prettify())path2=r'E:\work\FangCloudV2\personal_space\2learn\python3\html0003-1.html'
if not os.path.exists(path2): ? ? ? ? ? ? ?
? ? os.mkdir(path2)?with open(path2 ,"a") as f:
? ? f.write("測(cè)試bs4")
? ? f.write(soup1.prettify())
3.4 用 read() 打開(kāi) 本地 html 文件
3.4.1 語(yǔ)法差別?with open(path1 ,"r") as f:? ?和? res=f.read()
- 和? read()讀出來(lái)的內(nèi)容 (應(yīng)該和 requests.get()得出來(lái)得內(nèi)容一樣)
print ("對(duì)比測(cè)試requests")
with open(path1 ,"r") as f:
? ? res=f.read()
print (res)with open(path2 ,"a") as f:
? ? f.write("對(duì)比測(cè)試requests")
? ? f.write(res)
?
3.5 用requests打開(kāi) 本地 html 文件
- 沒(méi)試過(guò)
- 這種本體html沒(méi)法試把?
4? f.write(soup1.prettify()) 和 html 用 read()讀出來(lái)?差別很大
和? read()讀出來(lái)的內(nèi)容 (應(yīng)該和 requests.get()得出來(lái)得內(nèi)容一樣)
soup1.prettify()
5 其他
soup1.text ? 全部文本內(nèi)容?
soup1.a
soup1.find()
soup1.find_all()文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-480932.html
soup1.文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-480932.html
到了這里,關(guān)于python3 爬蟲(chóng)相關(guān)學(xué)習(xí)9:BeautifulSoup 官方文檔學(xué)習(xí)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!