python网络爬虫(一)
来源:互联网 发布:阿里云轻应用服务器 编辑:程序博客网 时间:2024/03/29 05:46
非结构化数据是数据结构不规则或不完整,没有预定义的数据模型,不方便用数据库二维逻辑表来表现的数据。包括所有格式的办公文档、文本、图片、XML, HTML、各类报表、图像和音频/视频信息等等。它没有固定的数据格式,不利用用户对数据进行使用与存储。
非结构化数据必须通过ETL工具将数据转换为结构化数据才能取用,将原始资料进行数据抽取,将其转换为ETL脚本进而完成从非结构化数据到结构化数据的转换,最后将解析好的数据存储起来。
网络爬虫架构如下所示:
过程为:网页链接器向网页服务器发出请求,网页服务器接收到请求之后对其进行处理,处理完成之后向网页解析器返回响应,网页解析器接收到响应之后对其中的数据内容进行分析、提取,得到结构化的数据信息,最后整合数据将其存储至数据库或数据中心中。
在编写网络爬虫代码之前,首先保证本机具有python的运行环境,并且具有以下的模块:
通过在cmd命令行模式下用pip包管理器工具安装即可,另外还需安装jupyter交互式运行环境,pip install jupyter命令可安装jupyter,安装完成之后直接键入jupyter notebook进入交互式运行环境
下面就可以进行代码编写了(Ctrl+Enter运行):
在页面中键入F12调出页面控制台,打开网络监听,刷新页面,对此次页面请求及其响应进行监听,在Doc中找到请求页面链接,单击连接,在header中找到请求地址和请求方式。
1.获取页面内容
import requestsnews = requests.get('http://news.sina.com.cn/china/')print(news.encoding)news.encoding = "utf-8"print(news.text)
运行结果:
在这里需要注意的是页面的编码格式默认为ISO-8859-1,所以在没有显示指定打印编码格式,内容中的中文会出现乱码情况,因此需要将文档的encoding设置为“utf-8”格式,乱码问题即可解决。
2.用BeautifulSoup剖析页面元素
from bs4 import BeautifulSouphtml_sample = '\<html>\<body>\<h1 id="title">hello world</h1>\<a href="#" class="link">this is link1</a>\<a href="# link2" class="link">this is link2</a>\</body>\</html>'soup = BeautifulSoup(html_sample,"html.parser")#将html文档转化为dom结构,便于之后的文档解析print(soup)print(soup.text)
运行结果:
3.BeautifulSoup基础操作
4.抓取新浪新闻信息
import requests#导入requests模块from bs4 import BeautifulSoup#导入BeautifulSoup模块new_url = requests.get('http://news.sina.com.cn/china/')#接收服务器响应new_url.encoding = "utf-8"#设置编码格式soup = BeautifulSoup(new_url.text,"html.parser")#将效应内容转化为dom结构,便于之后的文档解析# print(news.text)for news in soup.select('.news-item'):#获取clss=news-item标签全部内容 if(len(news.select('h2'))>0): h2 = news.select('h2')[0].text#获取标签名为h2并且内容不为空的内容 time = news.select('.time')[0].text#获取时间内容 a = news.select('a')[0]['href']#获取链接地址 print(h2,time,a)
运行结果:
5.抓取新闻内文标题
import requestsfrom bs4 import BeautifulSoupres = requests.get('http://news.sina.com.cn/o/2017-10-31/doc-ifynffnz3825090.shtml')res.encoding = 'utf-8'soup = BeautifulSoup(res.text,'html.parser')#soup与res.text的表现一样,但是结构不一致content = soup.select('#artibodyTitle')[0].texttime = soup.select('#navtimeSource')[0].texttimesource = soup.select('.time-source')[0].contents[0].strip()#如果抽取出来的内容中仍然包含标签,则用contents再次抽取,并用strip()忽略tab键print(timesource)print(time)print(content)print(soup)
运行结果:
6.抓取新闻发布时间
import requestsfrom bs4 import BeautifulSoupfrom datetime import datetime#导入datetime模块res = requests.get('http://news.sina.com.cn/o/2017-10-31/doc-ifynffnz3825090.shtml')res.encoding = 'utf-8'soup = BeautifulSoup(res.text,'html.parser')#soup与res.text的表现一样,但是结构不一致timesource = soup.select('.time-source')[0].contents[0].strip()dt = datetime.strptime(timesource,'%Y年%m月%d日%H:%M')#字符串转为时间,%Y年%m月%d日%H:%M为格式表示source = soup.select('.time-source span a')[0].textprint(source)td = dt.strftime('%Y-%m-%d')#时间转为字符串print(dt)print(td)
运行结果:
7.整理新闻内文
8.获取新闻编辑者名称
- 【网络爬虫】【python】网络爬虫(一):python爬虫概述
- python网络爬虫(一)
- python网络爬虫(一)
- python网络爬虫(一)
- 写网络爬虫学python(一)
- python 网络爬虫(一) 简单demo
- 【Python】网络爬虫(一):pyquery一瞥
- [Python]网络爬虫(一):一些基本概念
- 学习python写网络爬虫(一)
- 学习Python之网络爬虫(一)
- Python 网络爬虫学习(一)
- python网络爬虫实战笔记(一)
- Python网络爬虫(一):初步了解
- Python网络爬虫学习笔记(一)
- 【python网络爬虫一】爬虫工作原理
- Python之网络爬虫一
- Python实战(一)——Python编写网络爬虫
- Python 网络爬虫与信息获取(一)—— requests 库的网络爬虫
- 爬格子呀7-1
- 一、Docker 知识框架以及入门基础
- kafka学习心得
- 自学机器学习入门指南
- 机器学习心得之Andrew Ng(3)
- python网络爬虫(一)
- leetcode 47. Permutations II
- 常用端口号
- 揭秘UNIX文件缓存(the buffer cache)(实战篇)
- 我的电路实验
- Java(7-2 迭代器和链表详细说明)
- idea生成类注释和方法注释的正确方法
- 隐私政策
- 集合框架