一。request库
import json
import requests
from io import BytesIO
#显示各种函数相当于api
# print(dir(requests))
url = 'http://www.baidu.com'
r = requests.get(url)
print(r.text)
print(r.status_code)
print(r.encoding)
结果:
二。BeautifulSoup库
html:举例如下
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
解析代码如下:
from bs4 import BeautifulSoup
soup = BeautifulSoup(open('test.html'))
#使html文本更加结构化
# print(soup.prettify())
# Tag
print(type(soup.title))
结果:bs4的一个类