当前位置:网站首页>爬虫初始及项目
爬虫初始及项目
2022-06-22 05:30:00 【Amyniez】
1.robots.txt协议:
明确规定网站中那些数据可以被爬取,哪些不能被爬取(君子协议)
如输入:https://www.sohu.com/robots.txt来查看协议
2.http协议:
概念: 服务器与客户端交互的一种形式。类似于,杨子荣和座山雕之间的黑话,天王盖地虎,宝塔镇河妖(加密通话)
常用请求头信息:
- 1.User-Agent:请求载体表示的身份标识(用户代理)

Chrome浏览器中点击F12、network(网络),Ctrl+r,header,则可以得到图上所得的信息。
- 2.Connection:请求完毕后,是断开连接还是保持连接
常用响应头信息:
- Content-Type:服务器响应回客户端的数据类型
3.https协议:(s:security)
安全的超文本传输协议,即包括了数据加密
加密方式:
- 1.对称秘钥加密

弊端:秘钥可能会被第三方机构给拦截,造成加密信息泄露。
2.非对称秘钥加密
缺点:效率低,通讯速度减慢;公钥有可能被第三方更换3.证书秘钥加密

能够保证客户端所拿到的公钥一定是由服务器端产生的
4.requests模块
Python中原生的基于网络请求的模块,功能十分强大,简单便捷,效率极高,requests模块是爬虫的半壁江山
作用: 用来模拟浏览器发请求。
使用方式:
- 1.指定url(网址)
- 2.发起请求
- 3.获取响应数据
- 4.持久化存储
环境安装:
pip install -i http://pypi.douban.com/simple --trusted-host pypi.douban.com requests

import requests
# 指定url
url = 'https://www.baidu.com/'
# 发起请求
response = requests.get(url=url) # get方法会返回一个响应对象
# 获取响应数据
pageText = response.text # text返回的是字符串形式的响应数据
# 持久化存储
with open('C:\\Users\\Administrator\\Desktop\\baiduPachong.html','w',encoding='utf-8') as fp:
fp.write(pageText)
print("end")
4.网易云音乐爬虫
import requests
import re # 正则表达式,自带的,不需要安装
import os # 文件操作模块
filename = 'music1\\'
if not os.path.exists(filename):
os.mkdir(filename)
# 修改id可以访问不同的音乐榜单
url = 'https://music.163.com/#/discover/toplist?id=3778678'
# header请求头 就是伪装Python代码的 把Python代码伪装成浏览器对服务器进行访问
# 服务器接收到请求之后,会给我们返回响应数据(response)
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36'
}
response = requests.get(url = url,headers=headers)
#print(response.text)
# 正则表达式提取出来的一个内容 返回是列表 里面每一个元素都是元组
html_data = re.findall('<li><a href="/song\?id=(\d+)">(.*?)</a>',response.text)
for num_id, title in html_data:
# https://music.163.com/song/media/outer/url?id=1830419924.mp3
music_url = 'https://music.163.com/song/media/outer/url?id={num_id}.mp3'
# 对于音乐播放地址发送请求 获取二进制数据内容
music_content = requests.get(url=music_url, headers=headers).content
with open(filename + title + '.mp3',mode='wb') as f:
f.write(music_content)
print(html_data)
边栏推荐
- [issue 26] 123hr experience of Tencent teg+ operation development
- Research Report on demand and investment opportunities in key areas of global and Chinese strontium tungstate industry 2022-2027
- 机器学习笔记 八:Octave实现神经网络的手写数字识别
- 2022深圳福田区专精特新小巨人企业申报条件,补贴50万
- Tidb performance overview panel
- Kubernetes - deploy application to cluster
- Force buckle 33 [search rotation sort array]
- How wechat applet assigns values to sub components
- 参数序列化
- 旅行家的预算(洛谷)
猜你喜欢

Learning method 3 for promotion of large enterprises: chain learning method

通达OA漏洞分析合集

Yarn application submission process

Online text code comparison tool

2022 Shanxi secondary vocational group "Cyberspace Security" event module b- web page penetration

Rétroaction sur la remise des diplômes! Tous les contributeurs de la communauté Apache Doris sont ici pour recevoir des cadeaux!

总有人问我:独立站该怎么玩?3个案例,你看完就懂了

Summary of knapsack problem

SCM future employment development direction, learn SCM must know some entry-level knowledge and industry prospects, read the benefit

Kubernetes - deploy application to cluster
随机推荐
Delete the packaging use of pop-up components
错误:note: module requires Go 1.17
想投放Facebook广告却不知从何入手?此文带你深入了解
Online text code comparison tool
移动端布局适配
Sourcetree reported an error SSH failure
企业如何把ERP项目自动施行?
A simple method to implement deep cloning and encapsulation of objects JS
\[\e]0; \[email protected]\h: \w\a\]\[\033[01;32m\]\[email protected]\h\[\033[
关于二分一些模板
Implementation of lazy loading of pictures (summary and sorting)
Global and Chinese silicon carbide barrier Schottky diode market demand and future prospect report 2022-2027
Wanzi detailed data warehouse, data lake, data middle platform and lake warehouse are integrated
printf becomes puts
Performance analysis and test of interprocess communication methods under dual core real-time system
redis连接错误:ERR Client sent AUTH, but no password is set解决方案2个
Force buckle 33 [search rotation sort array]
Service migration when deploying SuperMap iserver war package
Xshell下载安装(解决评估过期问题)
机器学习笔记 六:逻辑回归中的多分类问题之数字识别