目录
1 BeautifulSoup 官方文档
2 用bs 和 requests 打开 本地html的区别:代码里的一段html内容
2.1 代码和运行结果
2.2 用beautiful 打开 本地 html 文件
2.2.1 本地html文件
2.2.2 soup1=BeautifulSoup(html1,"lxml")
2.3 用requests打开 本地 html 文件
2.3.1 本地html文件
2.3.2 print(html1)
3 用bs 和 requests 打开 本地html的区别:一个独立的html文件
3.1 独立创建一个html文件
3.2 下面是新得代码和运行结果
3.3 用beautiful 打开 本地 html 文件
3.3.1 语法差别 soup1=BeautifulSoup(open(path1))
3.4 用 read() 打开 本地 html 文件
3.4.1 语法差别 with open(path1 ,"r") as f: 和 res=f.read()
3.5 用requests打开 本地 html 文件
4 f.write(soup1.prettify()) 和 html 用 read()读出来 差别很大
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/
#E:\work\FangCloudV2\personal_space\2learn\python3\py0003.txt
import requests
from bs4 import BeautifulSoup
#html文件内容
html1 = """
The Dormouse's story
The Dormouse's story
Once upon a time there were three little sisters; and their names were
Elsie,
Lacie and
Tillie;
and they lived at the bottom of a well.
...
"""
#"测试bs4"
print ("测试bs4")
soup1=BeautifulSoup(html1,"lxml")
print (soup1.prettify())
#"对比测试requests"
print ("对比测试requests")
#res=requests.get(html1)
res=html1
#print (res.text)
print (res)
#"测试bs4"
html1=""" ... """
print ("测试bs4")
soup1=BeautifulSoup(html1,"lxml")
print (soup1.prettify())
lxml
html.parser
应该这几种都可以
#"对比测试requests"
print ("对比测试requests")
#res=requests.get(html1)
res=html1
#print (res.text)
print (res)
本地文件 html 已经是一段 脚本内的文本 """ ..."""
requests.exceptions.InvalidSchema: No connection adapters were found for '
The Dormouse\'s story
\n\nOnce upon a time there were three little sisters; and their names were\nElsie,\nLacie and\nTillie;\nand they lived at the bottom of a well.
\n\n...
\n'AttributeError: 'str' object has no attribute 'text'
代码
#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 ("测试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("测试bs4")
f.write(soup1.prettify())
print ("对比测试requests")
with open(path1 ,"r") as f:
res=f.read()
print (res)
with open(path2 ,"a") as f:
f.write("对比测试requests")
f.write(res)
"""
#地址,路径,前都记得加 r, 因为string 内部包含\/等转义符,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)
#本地地址不能像网址 url这样用,用的\/不同,即使用 raw r 也不行. 可以用转格式函数吗?
#https://www.baidu.com/
"""
运行结果
另存为的文件内容
最大的差别
path1=r"E:\work\FangCloudV2\personal_space\2learn\python3\html0003.html"
soup1=BeautifulSoup(open(path1))
print ("测试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("测试bs4")
f.write(soup1.prettify())
print ("对比测试requests")
with open(path1 ,"r") as f:
res=f.read()
print (res)with open(path2 ,"a") as f:
f.write("对比测试requests")
f.write(res)
和 read()读出来的内容 (应该和 requests.get()得出来得内容一样)
soup1.prettify()
5 其他
soup1.text ? 全部文本内容?
soup1.a
soup1.find()
soup1.find_all()
soup1.