首页 > 其他分享 >爬虫介绍

爬虫介绍

时间:2023-03-15 17:12:00浏览次数:27  
标签:http get res 爬虫 介绍 cookie print requests

爬虫介绍

爬虫是什么?
	-爬虫就是程序---》从互联网中,各个网站上,爬取数据[你能浏览的页面才能爬],做数据清晰
  
爬虫的本质
	-模拟http请求,获取数据---》入库
	-网站
	-app:抓包
  
补充:百度其实就是一个大爬虫
    -百度爬虫一刻不停的在互联网中爬取各个页面---》爬取完后---》保存到自己的数据库中
    -你在百度搜索框中搜索---》百度自己的数据库查询关键字---》返回回来
    -点击某个页面----》跳转到真正的地址上
    -seo:
    -sem:充钱的
   
我们所学习的
	-模拟发送http请求
	-requests模块
 	-selenium
 	-反爬:封ip,解决ip代理,封账号:解决cookie池
       
	-爬虫框架:scrapy

requests模块介绍

使用python如何发送http请求
模块:requests模块,封装了python内置模块urllib
    使用requests可以模拟浏览器的请求(http),比起之前用到的urllib,requests模块的api更加快捷(本质就是封装了urllib3)
   
安装:
    pip3 install requests

request发送get请求

import requests
#
# res = requests.get('https://www.cnblogs.com/liuqingzheng/p/16005866.html')
# print(res.text)


# 如果有的网站,发送请求,不返回数据,人家做了反扒---》拿不到数据,学习如何反扒
# res = requests.get('https://dig.chouti.com/')
# print(res.text)

requests携带参数

import requests

# 方式一:直接拼接到路径中
# res = requests.get('https://www.cnblogs.com/liuqingzheng/p/16005866.html?name=lqz&age=19')
# 方式二:使用params参数
res = requests.get('https://www.cnblogs.com/liuqingzheng/p/16005866.html',params={'name':"lqz",'age':19})
# print(res.text)
print(res.url)

url编码解码

import requests
from urllib.parse import quote,unquote
# res = requests.get('https://www.cnblogs.com/liuqingzheng/p/16005866.html',params={'name':"彭于晏",'age':19})
# # print(res.text)
#
# # 如果是中文,在地址栏中会做url的编码:彭于晏:%E5%BD%AD%E4%BA%8E%E6%99%8F
# print(res.url)
# 'https://www.baidu.com/s?wd=%E5%B8%85%E5%93%A5'


# 编码:
# res=quote('彭于晏')
# print(res)

# 解码
res=unquote('%E5%BD%AD%E4%BA%8E%E6%99%8F')
print(res)

携带请求头

反爬措施之一就是请求头

http请求中,请求头中有一个很重要的参数User-Agent
	-表明了客户端类型是什么:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36
 	-如果没有带这个请求头,后端就禁止
   -requests发送请求,没有携带该参数,所以有的网站就禁止了

import requests
# http请求头:User-Agent,cookie,Connection

# http协议版本间的区别
# Connection: keep-alive
# http协议有版本:主流1.1   0.9   2.x
# http 基于TCP 如果建立一个http链接---》底层创建一个tcp链接
# 1.1比之前多了keep-alive
# 2.x比1.x多了 多路复用
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'
}
res = requests.get('https://dig.chouti.com/',headers=headers)
print(res.text)

发送post请求,携带数据

import requests

# 携带登录信息,携带cookie
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36',
    'Cookie': ''
}
# post请求,携带参数
data = {
    'linkId': '38063872'
}
res = requests.post('https://dig.chouti.com/link/vote', headers=headers, data=data)
print(res.text)



# 双token认证

自动登录,携带cookie的两种方式

# 登录功能,一般都是post

import requests

data = {
    'username': '',
    'password': '',
    'captcha': '3456',
    'remember': 1,
    'ref': 'http://www.aa7a.cn/',
    'act': 'act_login'
}

res = requests.post('http://www.aa7a.cn/user.php',data=data)
print(res.text)
# 响应中会有登录成功的的cookie,
print(res.cookies)  # RequestsCookieJar 跟字典一样
# 拿着这个cookie,发请求,就是登录状态


# 访问首页,get请求,携带cookie,首页返回的数据一定会有 我的账号
# 携带cookie的两种方式  方式一是字符串,方式二是字典或CookieJar对象
# 方式二:放到cookie参数中
res1=requests.get('http://www.aa7a.cn/',cookies=res.cookies)

print('[email protected]' in res1.text)

requests.session的使用

为了保持cookie,以后不需要携带cookie

import requests

data = {
    'username': '',
    'password': '',
    'captcha': '3456',
    'remember': 1,
    'ref': 'http://www.aa7a.cn/',
    'act': 'act_login'
}

session = requests.session()
res = session.post('http://www.aa7a.cn/user.php', data=data)
print(res.text)
res1 = session.get('http://www.aa7a.cn/')  # 自动保持登录状态,自动携带cookie

print('[email protected]' in res1.text)

补充post请求携带数据编码格式

import requests

# data对应字典,这样写,编码方式是urlencoded
requests.post(url='xxxxxxxx',data={'xxx':'yyy'})
# json对应字典,这样写,编码方式是json格式
requests.post(url='xxxxxxxx',json={'xxx':'yyy'})
# 终极方案,编码就是json格式
requests.post(url='',
              data={'':1,},
              headers={
                  'content-type':'application/json'
              })

响应Response对象

# Response相应对象的属性和方法
import requests
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'
}
respone=requests.get('http://www.jianshu.com',headers=headers)
# respone属性
print(respone.text) # 响应体转字符串
print(respone.content) # 响应体的二进制内容

print(respone.status_code) # 响应状态码
print(respone.headers)   # 响应头
print(respone.cookies)  # cookie是在响应头,cookie很重要,它单独做成了一个属性
print(respone.cookies.get_dict()) # cookieJar对象---》转字典
print(respone.cookies.items())  # 一次性取出cooike的键值对

print(respone.url)    # 请求地址
print(respone.history) # 

print(respone.encoding)  # 响应编码格式

编码问题

# 有的网站,打印
res.text --->发现乱码---》请求回来的二进制---》转成了字符串---》默认用utf8转---》
response.encoding='gbk'
再打印res.text它就用gbk转码

下载图片,视频

import requests

# res=requests.get('http://pic.imeitou.com/uploads/allimg/230224/7-230224151210-50.jpg')
# # print(res.content)
# with open('美女.jpg','wb') as f:
#     f.write(res.content)
#


res=requests.get('https://vd3.bdstatic.com/mda-pcdcan8afhy74yuq/sc/cae_h264/1678783682675497768/mda-pcdcan8afhy74yuq.mp4')
with open('致命诱惑.mp4','wb') as f:
    for line in res.iter_content():
        f.write(line)

标签:http,get,res,爬虫,介绍,cookie,print,requests
From: https://www.cnblogs.com/zhanghong1229/p/17219203.html

相关文章

  • 爬虫介绍、requests模块发送get请求、get请求携带参数、携带请求头、携带cookie、发送
    目录今日内容1爬虫介绍2requests模块发送get请求3get请求携带参数4携带请求头5携带cookie6发送post请求7响应Response8获取二进制数据9解析json#期终架构 -后......
  • 知名第三方检测认证机构品牌介绍
    知名第三方检测认证机构品牌介绍下面带大家认识一下排名靠前的第三方(出场不分先后):SGS通标品牌介绍SGS(瑞士通用公证行),通标标准技术服务有限公司,1878年创立于法国,全球领先......
  • SDG,ADAM,LookAhead,Lion等优化器的对比介绍
    本文将介绍了最先进的深度学习优化方法,帮助神经网络训练得更快,表现得更好。有很多个不同形式的优化器,这里我们只找最基础、最常用、最有效和最新的来介绍。优化器首先,让......
  • Maven学习笔记1:Maven基本介绍和安装配置
    一、认识Maven官网http://maven.apache.org/上面有最权威的说明,其中包括下载、安装、运行示例,但是是英文版的。Maven是什么Maven是一个项目管理工具。它有何优点呢?......
  • MEGO灯盘简单介绍
    MEGO上面的灯盘怎么安装?有什么功能?对应的管脚怎么设置?灯盘正面背面接口说明:灯盘连接时需要使用配套的螺丝和尼龙柱进行固定和支撑,然后将对应的接口按照如图所......
  • 爬虫入门--xpatch
    XPath是一门在XML文档中查找信息的语言。XPath使用路径表达式在XML文档中进行导航。XPath包含一个标准函数库。XPath是XSLT中的主要元素。XPath是一个W3C......
  • JWT 认证和授权:详细介绍
    JSONWebToken(缩写JWT)是目前最流行的跨域认证解决方案,本文介绍它的原理和用法。一、跨域认证的问题互联网服务离不开用户认证。一般流程是下面这样。1、用户向服务器发送......
  • Kubernetes组件介绍
    一、APIserver1、APIServer是Kubernetes集群的网关,是能够与etcd通信惟一入口(1)kube-controller-manager、kube-scheduler、kubelet、kube-proxy,以及后续部署的集群插件Co......
  • 38 openEuler搭建FTP服务器-FTP总体介绍
    38openEuler搭建FTP服务器-FTP总体介绍38.1FTP简介FTP(FileTransferProtocol)即文件传输协议,是互联网最早的传输协议之一,其最主要的功能是服务器和客户端之间的文件传输......
  • nohup使用介绍
    什么是nohup?在Linux操作系统中,nohup是一个非常有用的命令,它允许用户在后台运行进程,即使在注销终端或关闭SSH连接后,进程也可以继续运行。本章将深入探讨nohup命令,包括其定......