首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Python爬虫知识点二

一。request库

代码语言:javascript
复制
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:举例如下

代码语言:javascript
复制
<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>

解析代码如下:

代码语言:javascript
复制
from bs4 import BeautifulSoup

soup = BeautifulSoup(open('test.html'))
#使html文本更加结构化
# print(soup.prettify())

# Tag

print(type(soup.title))
结果:bs4的一个类

下一篇
举报
领券