首页 > 其他分享 >爬虫从入门到入狱(一)

爬虫从入门到入狱(一)

时间:2022-11-23 21:23:04浏览次数:35  
标签:入门 get res 爬虫 data com print requests 入狱

今日内容概要

  • 爬虫介绍
  • requests模块发送get请求
  • get请求携带参数
  • 携带请求头
  • 携带cookie
  • 发送post请求
  • 响应Response
  • 获取二进制数据
  • 解析json

今日内容详细

爬虫介绍

爬虫:spider,网络蜘蛛

# 本质原理
    -现在所有的软件原理:大部分都是基于http请求发送和获取数据的
        -pc端的网页
        -移动端app
    -模拟发送http请求,从别人的服务端获取数据
    -绕过反扒:不同程度反扒措施不一样,比较复杂

# 爬虫原理
    发送http请求【requests,selenium】--->第三方服务端--->服务端响应数据解析出想要的数据【selenium,bs4】--->入库(文件,excel,mysql,redis,mongodb..)
    scrapy:专业的爬虫框架

# 爬虫是否合法
    爬虫协议:每个网站根路径下都有robots.txt,这个文件规定了该网站哪些可以爬取,哪些不能爬

# 百度:大爬虫
    -百度搜索框中输入搜索内容,回车,返回的数据,是百度数据库中的数据
    -百度一刻不停的在互联网中爬取各个页面,链接地址--->爬完存到自己的数据库
    -当你点击,跳转到真正的地址上去了
    -核心:搜索,海量数据中搜索出想要的数据
    -seo:免费的搜索,排名靠前
    -sem:花钱买关键字

requests模块发送get请求

# 模拟发送http请求的模块:requests不仅仅做爬虫用它,后期调用第三方接口,也是要用它的
# 安装
    pip3 install requests


# 代码演示
import requests
res=requests.get('https://www.cnblogs.com/liuqingzheng/p/16005866.html')
print(res.text) # http响应体的文本内容

get请求携带参数

方式一:地址栏中拼接

res=requests.get('https://www.cnblogs.com/?name=lqz&age=19')
print(res.text)

方式二:使用params参数

res=requests.get('https://www.cnblogs.com/',params={'name':'lqz','age':19})
print(res.text)

小注意:如果中文涉及到 url的编码和解码

from urllib import parse
res=parse.quote('美女')
print(res)
res=parse.unquote('%E7%BE%8E%E5%A5%B3')
print(res)

携带请求头

http 请求有请求头,有的网站通过某些请求头来做反扒,只要在请求头中添加user-agent参数即可,还有图片防盗链,还有除了首页外,上一个地址不是我们自己的地址,就禁掉。

#  请求头中带数据---->爬取某个网站,不能正常返回,模拟的不像
#  网站做反扒,禁掉没有携带请求头中的客户端类型
# User-Agent:客户端类型:有浏览器,手机端浏览器,爬虫类型,程序,scrapy。。一般伪造成浏览器
# referer:上次访问的地址:Referer: https://www.lagou.com/gongsi/
    # 如果要登录,模拟向登录接口发请求,正常操作必须在登录页面上才能干这事,如果没有携带referer,它就认为你是恶意的,拒绝调用
    # 图片防盗链

header={
    # 客户端类型
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'}
res=requests.get('https://dig.chouti.com/',headers=header)
print(res.text)

携带cookie

方式一:直接带在请求头中
# 模拟点赞
data={
    'linkId':'36996038'
}
header={
    # 客户端类型
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36',
    #携带cookie
'Cookie':''
}
res=requests.post('https://dig.chouti.com/link/vote',data=data,headers=header) print(res.text)


方式二:通过cookie参数:因为cookie很特殊,一般都需要携带,模块把cookie单独抽取成一个参数,是字典类型,以后可以通过参数传入
data={
    'linkId':'36996038'
}
header={
    # 客户端类型
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36',
}
res=requests.post('https://dig.chouti.com/link/vote',data=data,headers=header,cookies={'key':'value'})
print(res.text)

发送post请求

# 发送post请求
data = {
    'username': '[email protected]',
    'password': 'lqz123',
    'captcha': 'cccc',
    'remember': 1,
    'ref': 'http://www.aa7a.cn/',
    'act': 'act_login'
}
res = requests.post('http://www.aa7a.cn/user.php', data=data)
print(res.text)
print(res.cookies)  # 响应头中得cookie,如果正常登录,这个cookie 就是登录后的cookie CookieJar:当成字典
# 访问首页,携带cookie,
# res2 = requests.get('http://www.aa7a.cn/', cookies=res.cookies)
res2 = requests.get('http://www.aa7a.cn/')
print('[email protected]' in res2.text)

# post请求携带数据 data={} , json={}   drf后端,打印 request.data
# data=字典是使用默认编码格式:urlencoded
# json=字典是使用json 编码格式
res = requests.post('http://www.aa7a.cn/user.php', json={})

# request.session的使用:当request使用,但是它能自动维护cookie
session=requests.session()
data = {
    'username': '[email protected]',
    'password': 'lqz123',
    'captcha': 'cccc',
    'remember': 1,
    'ref': 'http://www.aa7a.cn/',
    'act': 'act_login'
}
res = session.post('http://www.aa7a.cn/user.php', data=data)
res2 = session.get('http://www.aa7a.cn/')
print('[email protected]' in res2.text)

响应Response

# Response对象,有很多属性和方法
import requests

header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36',
}
respone = requests.get('https://www.jianshu.com', params={'name': 'lqz', 'age': 19},headers=header)
# respone属性
print(respone.text)  # 响应体的文本内容
print(respone.content)  # 响应体的二进制内容
print(respone.status_code)  # 响应状态码
print(respone.headers)  # 响应头
print(respone.cookies)  # 响应cookie
print(respone.cookies.get_dict())  # cookieJar对象,获得到真正的字段
print(respone.cookies.items())  # 获得cookie的所有key和value值
print(respone.url)  # 请求地址
print(respone.history)  # 访问这个地址,可能会重定向,放了它冲定向的地址
print(respone.encoding)  # 页面编码

获取二进制数据

# 获取二进制数据 :图片,视频
res = requests.get(
'https://upload.jianshu.io/admin_banners/web_images/5067/5c739c1fd87cbe1352a16f575d2df32a43bea438.jpg')
with open('美女.jpg', 'wb') as f:
    f.write(res.content)

# 一段一段写

res=requests.get('https://vd3.bdstatic.com/mda-mk21ctb1n2ke6m6m/sc/cae_h264/1635901956459502309/mda-mk21ctb1n2ke6m6m.mp4')
with open('美女.mp4', 'wb') as f:
    for line in res.iter_content():
        f.write(line)

解析json

# 前后分离后,后端给的数据,都是json格式,
# 解析json格式
res = requests.get(
    'https://api.map.baidu.com/place/v2/search?ak=6E823f587c95f0148c19993539b99295&region=%E4%B8%8A%E6%B5%B7&query=%E8%82%AF%E5%BE%B7%E5%9F%BA&output=json')
print(res.text)
print(type(res.text))
print(res.json()['results'][0]['name'])
print(type(res.json()))

标签:入门,get,res,爬虫,data,com,print,requests,入狱
From: https://www.cnblogs.com/wwjjll/p/16920149.html

相关文章

  • python入门基础之数据库
    python入门基础之数据库目录python入门基础之数据库字符编码与配置文件存储引擎主要的四个存储引擎创建表的完整语法字段类型之整型严格模式字段类型之浮点型字段类型之字......
  • 真的,Java并发编程入门看这个就够了
    Java并发编程学习之02Java并发编程入门指南(真的,Java并发编程入门看这个就够了)1.Java天生多线程importjava.lang.management.ManagementFactory;importjava.lang......
  • 爬虫基础
    爬虫介绍1.爬虫1.1什么是爬虫通过编写程序,模拟浏览器上网,然后让其去互联网上抓取数据的过程1.2现在所有的软件原理:大部分都是基于http请求发送和获取数据的-pc端的网......
  • 【2022-11-23】爬虫从入门到入狱(一)
    一、爬虫介绍#爬虫介绍: 网络爬虫(webcrawler)又称为网络蜘蛛(webspider)或网络机器人(webrobot),另外一些不常使用的名字还有蚂蚁、自动索引、模拟程序或蠕虫,同时它也是“物联......
  • 真的,Java并发编程基础入门看这个就够了
    Java并发编程学习之02Java并发编程入门指南@目录1.Java天生多线程2.Java启动多线程实现方式2.1实现代码2.2Thread和Runnable的区别2.3start和run方法的区别3.Java......
  • 爬虫之简介
    一、爬虫简介#爬虫:spider,网络蜘蛛#本质原理:-现在所有的软件原理:大部分都是基于http请求发送和获取数据的-pc端的网页-移动端app-模拟......
  • 爬虫学习-01
    一、爬虫介绍爬虫(spider):网络蜘蛛本质原理:现在所有的软件原理:大部分都是基于http请求发送和获取数据的PC端的网页移动端app模拟发送http请求,从别人的服务端获取数......
  • Python 爬虫2例:爬网络小说
    程序逻辑:按给出的第一章节URL,抓HTML,然后通过正则表达式,取出小说章节的标题、正文、下章节的URL,然后跳转到下一章节,不断循环处理。取出的正文写入文本文件。同时记录每......
  • Python 爬虫:喜马拉雅FM音频(这个还能不载,试了一下其它下载工具都不行了)
    现在是3:35分,花了整晚时间,写了个喜马拉雅FM音频爬虫,顺便熟悉了一下tkinterGUI效果图:已打包的工具可以在下面下载:链接:​​https://pan.baidu.com/s/1jJ50I4vlJyRtVFXR......
  • python 爬虫 试了五种爬喜马的方法并做了测试代码(爬虫系列研究告一段落了...)
    先介绍一下我想到和测试了的五种方法: 方法1,使用下面接口:    通过web_api'http://m.ximalaya.com/m-revision/page/album/queryAlbumPage/%s?pageSize=1000'%al......