-
2,爬取新闻网站新闻列表
-
3,爬取图片
-
4,把爬取到的数据存在本地文件夹或者数据库
-
5,学会用pycharm的pip安装Python需要用到的扩展包
1,准备工作
项目用的BeautifulSoup4和chardet模块属于三方扩展包,如果没有请自行pip安装,我是用pycharm来做的安装,下面简单讲下用pycharm安装chardet和BeautifulSoup4
-
在pycharm的设置里按照下图的步骤操作
-
如下图搜索你要的扩展类库,如我们这里需要安装chardet直接搜索就行,然后点击install package, BeautifulSoup4做一样的操作就行
- 安装成功后就会出现在在安装列表中,到此就说明我们安装网络爬虫扩展库成功
我们这里以抓取简书首页为例:http://www.jianshu.com/
# 简单的网络爬虫
from urllib import request
import chardet
response = request.urlopen("http://www.jianshu.com/")
html = response.read()
charset = chardet.detect(html)# {'language': '', 'encoding': 'utf-8', 'confidence': 0.99}
html = html.decode(str(charset["encoding"])) # 解码
print(html)
由于抓取的html文档比较长,这里简单贴出来一部分给大家看下
<!DOCTYPE html>
<!--[if IE 6]><html class="ie lt-ie8"><![endif]-->
<!--[if IE 7]><html class="ie lt-ie8"><![endif]-->
<!--[if IE 8]><