一、涉及的Python库
requests:获取网页源代码
BeautifulSoup:从网页中抓取数据
xlwt:导出表格
(一)requests
1.requests库文档:
requests库文档链接
2.request库的常用方法:
3.编写代码
#导入requests模块
import requests
#输入想获取的网页
url = 'https://movie.douban.com/chart'
#创建一个名为html的response对象
html = requests.get(url)
#设置系统默认编码为UTF-8,防止乱码
html.encoding='utf-8'
(二)beautifulsoup
1.简介:Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库,它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式
2.beautifulsoup官方文档:
Beautiful Soup 4.4.0 文档链接
3.基本操作
from bs4 import BeautifulSoup
#使用BeautifulSoup解析这段代码,得到一个BeautifulSoup对象
soup=BeautifulSoup(html,'html.parser')
#按照标准的缩进格式的结构输出
print(soup.prettify()