python学习之爬虫初体验
2024-10-18 05:11:33
作业来源: "https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2851" **
1.简述爬虫原理
通用爬虫
即(搜索引擎),通过各站点主动提交域名等信息,或与DNS服务商合作,爬取大部分站点信息
聚焦爬虫
通过模拟用户(即客户端浏览器)访问服务器的行为,从而达到欺骗服务器,获取数据。
2.理解(聚焦)爬虫开发过程
发起请求
向目标服务器发送一个伪造的请求报文
获取响应
得到服务器响应的数据
解析内容
将得到的数据按一定方式解析
保存数据
将解析后的数据收录入文本文件或数据库
浏览器工作原理:
向服务端发送请求报文,收到响应报文后解析其中数据,缓存部分数据。
抓取网站
使用第三方库requests
url = "http://news.gzcc.cn/html/xiaoyuanxinwen";
def use_requests(url):
'''
使用到了第三方库requests获取响应
'''
response = requests.get(url);
response.encoding = "utf-8";
return response;运行结果
使用python自带库 urllib
def use_urllib(url):
'''
使用到了自带urllib获取响应
'''
response = request.urlopen(url);
return response;
一个简单的html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>这是一个简单的网页</title>
<!--简单的样式定义-->
<style>
.class1 {
background: green;
}
.ckass2 {
background: yellow;
}
</style>
</head>
<body>
<div class="class1">
<strong id="strong">这是一个粗体标签</strong><br/>
<b id="b">这依旧是一个粗体标签</b><br/>
<big id="big">这貌似也是一个粗体标签</big><br/>
</div>
<div class="ckass2">
<del id="del">这是一个删除线</del><br/>
<s id="s">这也是一个删除线</s><br/>
<strike id="strike">这同样是一个删除线</strike><br/>
</div>
</body>
</html>
使用BeautifulSoup解析网页
from bs4 import BeautifulSoup
with open(r'simple.html','r',encoding='utf-8') as f:
text = f.read()
dom_tree = BeautifulSoup(text, 'html.parser');
from_label = dom_tree.select('strong')[0].text;
from_class = dom_tree.select('.class1')[0].text;
from_id = dom_tree.select('#strong')[0].text;
print(from_label, from_class, from_id);
提取新闻
dom_tree = BeautifulSoup(use_requests("http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0320/11029.html").text,
'html.parser');
title_from_class = dom_tree.select(".show-title")[0].text;
print(title_from_class);
infos_from_class = dom_tree.select(".show-info")[0].text;
list = infos_from_class.split()[0:-1];
for i in list:
print(i);
最新文章
- Linux文件与目录管理
- 转载:Hadoop排序工具用法小结
- 【MySQL】事务没有提交导致 锁等待Lock wait timeout exceeded异常
- Android 开发框架
- ccnu-线段树联系-单点更新2-B
- Hadoop基于Protocol Buffer的RPC实现代码分析-Server端
- Python 中模块间全局变量的使用上的注意
- 15-C语言结构体
- CSS 实现自动换行、强制换行、强制不换行的属性
- 4. svg学习笔记-文档结构元素和样式的使用
- Python魔法函数
- 解决win10休眠后无法唤醒
- Spring 基础使用
- 6.ZigZag Conversion(Graph, traverse)
- 记点事! oracle 调用外部命令
- CodeForces 558D
- ABAP术语-Call Transaction
- Leecode刷题之旅-C语言/python-14.最长公共前缀
- 如何设置Google Chrome的界面显示语言
- win10 U盘重装