首页 > 其他分享 >爬虫操作

爬虫操作

时间:2023-03-15 20:57:16浏览次数:29  
标签:www http 请求 res 爬虫 print 操作 requests

目录

爬虫操作

import requests

模块 模拟网络请求

 res = requests.get('https://www.cnblogs.com/moongodnnn/p/17219288.html')
 print(res.text)
# 基础用法,爬取网页全部html内容



# 请求头携带参数 方式1 直接加在后面
res = requests.get('https://www.cnblogs.com/moongodnnn/?page=3')

res = requests.get('https://www.cnblogs.com/moongodnnn/', params={'page': '3'})
print(res.url)
# 也可以通过 params={K:V,K:V} 
# https://www.cnblogs.com/moongodnnn/?page=3

url网址中文编码解码

res = requests.get('https://www.cnblogs.com/moongodnnn/', params={'name': '吴彦祖'})
print(res.url)
# https://www.cnblogs.com/moongodnnn/?name=%E5%90%B4%E5%BD%A6%E7%A5%96
# 链接中携带的中文数据 会被编码 需要解码


from urllib.parse import quote,unquote

name = unquote('%E5%90%B4%E5%BD%A6%E7%A5%96')
# name = 彭于晏
# 解码 把url中的编码转为utf8

name1 = quote('彭于晏')
# name1 = %E5%90%B4%E5%BD%A6%E7%A5%96
# 编码 把中文转为url编码格式
print(name,name1)

发送请求携带请求头

真实浏览器访问接口也会自带一些 请求头数据

爬虫也需要模仿这些 所以要在请求头中加入数据

需要携带 user-agent 还有 cookie

# 反扒措施之一,就是请求头

# 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
    -如果没有带这个请求头,后端就禁止
    -request发送请求,没有携带该参数,所以有的网站就禁止了
    
    
    

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

headers = {
    'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Mobile Safari/537.36',
    'Cookie': 'deviceId=web.eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJqaWQiOiIxMmEzMDM3ZC1kYTAxLTQ5OWYtOThjYS02NGQ2YjQ0ODIzYTYiLCJleHBpcmUiOiIxNjgxNDcxMzIxNDc5In0.wC9lWJTMmjd5B80VqQ_5IszO8fx__GS2izwseH7-82Y; __snaker__id=CzPxzJZTLtU5R6HD; gdxidpyhxdE=sA3CG62%2FwKQDDqRSP372dmqn%5ChUTAZX1xC5%5Czy2%5CB%2Fn5GoBQhCcvLXXT3vsKWY4epI2y9tAuEQ%2FO6I7gtt4La8T33wy7N%2BkeYNu7APU32xp'
}
# 请求头携带 Cookie 这样就是已登录状态

data = {
    'linkId': '38068398'
}
# 给接口携带的数据


res = requests.post('https://dig.chouti.com/link/vote', headers=headers, data=data)
print(res.text)
# 拒接爬虫访问 需要携带 请求头和数据
# {"msg":"你已经推荐过了","code":400,"errorType":0,"success":false}
# 自动点赞实现


扩展:如果需要解决爬虫问题,可以使用双token认证 

模拟自动登录

先找到网页的登录接口,查看需要传递什么参数

#
import requests

data = {
    'username': '[email protected]',
    'password': 'lqz123',
    '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)
# 获取到cookies

res = requests.post('http://www.aa7a.cn/user.php',cookies=res.cookies)
# 这样就可以 已登录用户的身份访问其他接口

Requests.session

为了保持cookie 自动维护cookies

import requests

data = {
    'username': '[email protected]',
    'password': 'lqz123',
    '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)
# 当有了cookies以后会自动维护 自动保持登录状态
print(res.text)
print(res.cookies)
# 获取到cookies

res = session.post('http://www.aa7a.cn/user.php')
# 直接用session对象请求 这样自动会带着cookies去请求,


只需要获取一次cookies即可

post请求携带数据编码格式

大部分接口需要我们提交json格式数据

import requests

# data对应字典,这样写,编码方式是urlencoded
requests.post(url='xxxxxxxx',data={'xxx':'yyy'})


# json对应字典,这样写,编码方式是json格式
requests.post(url='xxxxxxxx',json={'xxx':'yyy'})



# 终极方案,编码就是json格式  只需要在请求头中加上'content-type':'application/json'
# 指定编码格式
requests.post(url='',
              data={'':1,},
              headers={
                  'content-type':'application/json'
                
              })


返回的response中有哪些数据


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())  # cookie的键值对

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

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

编码问题

# 有的网站,打印
res.text
拿回来的数据是二进制数据 二进制的字符串,并且默认用utf8格式进行了转码

# 如何解决
response.encoding='gbk'
这样就指定了编码格式更换,
但是一般网站都是utf8就可以的

爬虫下载图片视频

无防盗链公开资源

import requests

res = requests.get('http://pic.imeitou.com/uploads/allimg/230224/7-230224151210-50.jpg')

with open('美女头像.jpg', 'wb') as f:
    f.write(res.content)
    # 获取二进制数据 并写入到本地
    print('下载完成')

# 直接将图片写入到我们本地

src = "https://vd2.bdstatic.com/mda-pcdgg1sfdavt6g1e/720p/h264/1678795145924710807/mda-pcdgg1sfdavt6g1e.mp4"
res = requests.get(src)

with open('汽车.mp4', 'wb') as f:
    for line in res.iter_content():
        f.write(line)
    # 因为视频可能很大,所以这样一行行的写入
    print('视频下载完成')

标签:www,http,请求,res,爬虫,print,操作,requests
From: https://www.cnblogs.com/moongodnnn/p/17219973.html

相关文章

  • 爬虫
    今日内容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.1.3操作系统的发展与分类(多道批处理 单道批处理系统 分时操作系统 实时操作系统)
    目录​​1.知识总览​​​​2.手工操作阶段​​​​3.单道批处理系统​​​​4.多道批处理系统​​​​5.多道批处理系统和单道批处理系统比较​​​​6.分时操作系统​​​......
  • 1.1.4操作系统(运行机制与体系结构)
    目录​​1.目录​​​​2.运行机制​​​​ 3.操作系统内核​​​​4.操作系统的体系结构​​1.目录2.运行机制   3.操作系统内核  4.操作系统的体系结构    ......
  • 爬虫简介
    1爬虫介绍#爬虫是什么 -爬虫就是程序---》从互联网中,各个网站上,爬取数据[你能浏览的页面才能爬],做数据清洗,入库#爬虫的本质 -模仿方式http请求,获取数据---》入......
  • 爬虫学习02之模拟登录以及代理
    一、调试模式介绍调试模式,即进入网页页面半代码模式,查看网页与代码一一对应关系。鼠标右键,再出现选项中找到检查进入调试模式,或者按键盘上的F12键进入调试模式。功能介......
  • 爬虫01
    目录1爬虫介绍2requests模块介绍3requests发送get请求4request携带参数5url编码解码6携带请求头7发送post请求,携带数据8自动登录,携带cookie的两种方式9requests.......
  • 第76课:Spark SQL基于网站Log的综合案例实战之Hive数据导入、Spark SQL对数据操作每天
    /***王家林老师授课​​http://weibo.com/ilovepains​​*/ 每天晚上20:00YY频道现场授课频道68917580 第76课:SparkSQL基于网站Log的综合案例实战之Hive数据导入、Spa......
  • SPARK 使用Java 在IDE中实战RDD和DataFrame动态转换操作
    /***王家林老师授课​​​http://weibo.com/ilovepains​​*/ 源文件1,Spark,72,Hadoop,113,Flink,5 运行结果 ......
  • 爬虫-requests
    1.爬虫介绍爬虫就是程序从互联网中各个网站上爬取数据(只能爬区我们能看到的数据),然后进行数据清洗(筛选),入库。爬虫的本质:模拟http请求,获取数据,入库。百度就是一个大......