2020.06.14 9.02
# 从html文件中加载到python中
with open("page_source.html", mode='r', encoding='utf-8') as fp:
soup = BeautifulSoup(fp, 'html.parser')
print(soup)
# 从网站中请求html文件
import requests
response = requests.get("http://python123.io/ws/demo.html")
html = response.text
print(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类型
print(type(html))
<class 'str'>
附:
示例:<p class=“title”>… </p>
基本元素 | 说明 |
---|---|
Tag | 标签,分别用<>和</>标明开头和结尾 |
Name | 标签名,<p>…</p>的名字是'p',格式:<ta>.name |
Attributes | 标签的属性,字典形式组织,格式:<tag>.attrs |
NavigableString | 标签内非属性字符串,<>…</>中字符串,格式:<tag>.string |
Comment | 标签内字符串的注释部分,一种特殊的Comment类型 |