Python网络爬虫学习笔记——第一个爬虫程序

运行环境

语言

  • Python3

第三方库

pip install reqeusts
pip install BeautifulSoup4
pip install jupyter

在线编辑器

安装 jupyter 模块后,在cmd窗口中运行命令jupyter notebook 就会自动在浏览器中打开一个在线编辑器。如下:在这里插入图片描述
打开右上角的 new 列表,选中python3,就会打开编辑界面,就可以在里面编辑Python代码啦。在这里插入图片描述

第一个爬虫

import requestes
# 爬取页面的路径
url = 'https://www.sina.com.cn/'
# 发送get请求并得到相应
res = requests.get(url)
# 设置页面编码为utf-8 否则得到的页面会出现乱码
res.encoding = 'utf-8'
# 输出页面内容
print(res.text)

编写了上面代码后再按页面热键 ctrl+enter 执行代码就能得到新浪网页的内容了。
爬取效果
这样就是一个简单的爬取新浪网页的爬虫了。当然,网路爬虫不可能只是那么简单,持续学习中。!!!

猜你喜欢

转载自blog.csdn.net/weixin_42600599/article/details/84860511