首页 > 其他分享 >爬虫初识

爬虫初识

时间:2023-03-15 21:36:07浏览次数:35  
标签:http res 爬虫 --- 初识 cookie print requests

目录

爬虫初识

昨日回顾

# 1 上线架构图

# 2 购买云服务器

# 3 远程链接云服务器
	xshell
    fianlshell

# 4 安装mysql
	下载rpm包
    安装
    修改root用户密码
    
# 5 redis 源码
	在不同平台编译
    make make install
    做软链接
    
# 6 python3.8
	安装前置软件
    下载源码包
    解压
    
# 7 安装uwsgi

# 8 安装虚拟环境
	workon 虚拟环境名字 ---> 进入虚拟环境
    deactivate 虚拟环境名字 ---> 退出虚拟环境
    
# 9 安装nginx
	1 启动
    	nginx
        
    2 关闭nginx
    	nginx -s stop
        
    3 重启nginx
    	nginx -s reload
        
    4 查看端口 强行关闭
    	ps -aux|grep nginx  ---> 过滤并查看端口
        kill <pid:进程编号>
        
# 10 上线前端
	编译vue: npm run build ---> dist文件夹下
    传到服务器:lrzsz
    解压zip:unzip
    copy到 /home/html
    
    修改nginx配置文件 让它能够代理前端静态文件
    
# 11 后端上线
	修改后的代码 提交到git上
    云服务器: git clone下来
    	logs文件必须有 因为要用到 不然报错
        
    安装所有依赖:先装能装的
    uwsgi上线:虚拟环境也要装
    uwsgi配置文件
    
    nginx 增加一个新增的server
    
# 12 数据库配置
	创建数据库
    新增用户 设置用户权限
    录入数据
    
# 13 处理前端静态文件
	STAIC_ROOT = ''
    python manage_pro.py collectstatic
    
    nginx代理
    
# 14 域名解析


# 开源项目赏析
	https://gitee.com/huap/projects

今日内容详细

1 爬虫介绍

# 爬虫是什么
	爬虫就是程序---> 从互联网中 各个网站上 爬取数据【你能浏览的页面才能爬】 做数据清洗 入库
    
# 爬虫的本质
	模拟方式http请求 获取数据---> 入库
    	网站
        app:抓包
        
# 补充:百度就是一个大爬虫
	百度爬虫一刻不停的在互联网中爬取各个页面---> 爬取完后---> 保存到自己的数据库中
    你在百度搜索框中搜索---> 百度自己的数据库查询关键字---> 返回数据回来
    点击某个页面---> 跳转到真正的地址上
    seo:SEO,全称是搜索引擎优化(Additional Search Optimization,简称 SEO),是指搜索引擎优化系统借助技术和算法改进,实现网站优化工作目标的一种推广手段。其本质是利用搜索引擎(包括百度、谷歌、搜狗等)搜索用户想要浏览的网页,然后通过技术将其展示给目标用户,让用户找到该页面,并实现转化
    sem:充钱的 搜索引擎营销:英文Search Engine Marketing ,我们通常简称为“SEM”。简单来说,搜索引擎营销就是基于搜索引擎平台的网络营销,利用人们对搜索引擎的依赖和使用习惯,在人们检索信息的时候将信息传递给目标用户。搜索引擎营销的基本思想是让用户发现信息,并通过点击进入网页,进一步了解所需要的信息。企业通过搜索引擎付费推广,让用户可以直接与公司客服进行交流、了解,实现交易。
    
# 咱们学习的
	模拟发送http请求
    	requests模块
        selenium
        反扒:封ip:ip代理 封账号:cookie池
        
    解析数据:bs4
    入库:mysql redis 文件中
    
    爬虫框架:scrapy

2 request模块介绍

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

3 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)

4 request携带参数

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)

5 url编码解码

import requests
from urllib.parse import quote, unquote

res = requests.get('https://www.cnblogs.com/liuqingzheng/p/16005866.html',params={'name':"彭于晏",'age':19})

# 如果是中文 在地址栏中会做url的编码:彭于晏:%E5%BD%AD%E4%BA%8E%E6%99%8F
print(res.url)  # https://www.cnblogs.com/liuqingzheng/p/16005866.html?name=%E5%BD%AD%E4%BA%8E%E6%99%8F&age=19

# 编码:
res1 = quote('彭于晏')
print(res1)  # %E5%BD%AD%E4%BA%8E%E6%99%8F
# 解码
res2 = unquote(res1)
print(res2)  # 彭于晏

6 携带请求头

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

# http协议版本间的区别
# Connection:keep-alive
# http协议有版本:主流1.1 0.9 2.x  新出的有3.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)
# 就可以请求成功

7 发送post请求 携带数据

import requests

# 携带登录信息 携带token
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认证

8 自动登录 携带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)  # True则证明登录成功

9 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)xxxxxxxxxx # 为了保存cookie 以后不需要携带cookie# 为了保存cookie 以后不需要携带cookieimport requestsdata = {    '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/')  # 自动保持登录状态 自动携带cookieprint('[email protected]' in res1.text)

10 补充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'})

11 响应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'
}

response = requests.get('http://www.jianshu.com',headers=headers)
# response属性
print(response.text)  # 响应体转成了字符串
print(response.content)  # 响应体的二进制内容

print(response.status_code)  # 响应状态码
print(response.headers)  # 响应头
print(response.cookies)  # cookie是在响应头 cookie很重要 它单独做成了一个属性
print(response.cookies.get_dict())  # cookieJar对象---> 转成字段
print(response.cookies.items())  # cookie的键值对

print(response.url)  # 请求地址
print(response.history)  # 不用关注

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


12 编码问题

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

13 下载图片 视频

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,res,爬虫,---,初识,cookie,print,requests
From: https://www.cnblogs.com/zpf1107/p/17220133.html

相关文章

  • 爬虫操作
    目录爬虫操作url网址中文编码解码发送请求携带请求头发送post请求给接口模拟自动登录Requests.sessionpost请求携带数据编码格式返回的response中有哪些数据编码问题爬虫下......
  • 爬虫
    今日内容1.爬虫介绍2.request模块介绍3.request发送get请求4.request携带参数5.url编码解码6.携带请求头7.发送post请求,携带数据8.自动登录,携带cookie的两种方式......
  • 【python爬虫】 request模块介绍 http协议版本区别 双token认证 携带cookie的两种方
    目录上节回顾今日内容1爬虫介绍2request模块介绍3request发送get请求4request携带参数5url编码解码6携带请求头http协议版本之间的区别7发送post请求,携带数据对于......
  • 爬虫简介
    1爬虫介绍#爬虫是什么 -爬虫就是程序---》从互联网中,各个网站上,爬取数据[你能浏览的页面才能爬],做数据清洗,入库#爬虫的本质 -模仿方式http请求,获取数据---》入......
  • 爬虫学习02之模拟登录以及代理
    一、调试模式介绍调试模式,即进入网页页面半代码模式,查看网页与代码一一对应关系。鼠标右键,再出现选项中找到检查进入调试模式,或者按键盘上的F12键进入调试模式。功能介......
  • 爬虫01
    目录1爬虫介绍2requests模块介绍3requests发送get请求4request携带参数5url编码解码6携带请求头7发送post请求,携带数据8自动登录,携带cookie的两种方式9requests.......
  • 爬虫-requests
    1.爬虫介绍爬虫就是程序从互联网中各个网站上爬取数据(只能爬区我们能看到的数据),然后进行数据清洗(筛选),入库。爬虫的本质:模拟http请求,获取数据,入库。百度就是一个大......
  • 爬虫介绍
    爬虫介绍爬虫是什么? -爬虫就是程序---》从互联网中,各个网站上,爬取数据[你能浏览的页面才能爬],做数据清晰爬虫的本质 -模拟http请求,获取数据---》入库 -网站 -......
  • 爬虫介绍、requests模块发送get请求、get请求携带参数、携带请求头、携带cookie、发送
    目录今日内容1爬虫介绍2requests模块发送get请求3get请求携带参数4携带请求头5携带cookie6发送post请求7响应Response8获取二进制数据9解析json#期终架构 -后......
  • 初识Node和内置模块
    初识Node与内置模块概述:了解Node.js,熟悉内置模块:fs模块、path模块、http模块初识Node.js浏览器中的JavaScript运行环境运行环境是指代码正常运行所需的必要环境对于C......