ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

爬虫

2022-06-22 08:02:21  阅读:175  来源: 互联网

标签:respone res 爬虫 data print requests com


spider

简介:

浏览器发送http请求,去后端服务器获取到数据之后只能从浏览器中看,如果要把需要的数据保存到本地,存到我们自己库中就可以用到爬虫

百度本质就是一个大爬虫(搜索),在输入框中输入搜索内容,实际是从百度的数据库搜索出来的

百度数据库的数据是从互联网爬下来的,百度这个爬虫一刻不停的在互联网爬数据,爬完就存到它的库里(seo优化,优化我们的网站能够被搜索引擎先搜到,排的很靠前),尽可能被百度爬虫,并且容易搜索到,seo(免费的)和sem(充钱,把你放前面)---》,当时爬虫爬取这个网页这一刻,网页的样子---》保留这个网页地址,当你点击标题,跳转到这个网页,完成了你的搜索 伪静态

爬虫的本质----》模拟发送http请求(浏览器携带什么,我们也要携带什么)---->服务器返回数据---》对数据进行清洗---》入库 后续操作是别的---》分析数据--》数据分析

爬虫协议:君子协议---》大家遵循这个协议,就不违法--》规定了我的网站,什么能爬,什么不能爬

https://www.baidu.com/robots.txt
https://www.cnblogs.com/robots.txt

爬虫本质跟用浏览器访问没什么区别

requests模块介绍,发送get请求

# 所有语言都可以做爬虫---》python简单一些---》库多
# 模拟发送http请求的库,requests库----》大佬基于python内置库 urllib(麻烦) 封装--》requests

# 安装
pip3 install requests

# 模拟发送get请求
import requests

res=requests.get('https://www.cnblogs.com/liuqingzheng/p/16005866.html')
print(res.text)  # 响应体的内容打印出来了

get地址中携带参数

## 2 get 请求中携带参数
res=requests.get('https://www.cnblogs.com/liuqingzheng/p/16005866.html',
                 params={'name':'lqz','age':18}
                 )
# # ?name=lqz&age=18拼到路径后面
print(res.text)  # 响应体的内容打印出来了
# url编码和解码
https://www.baidu.com/baidu?wd=python%20url%E7%BC%96%E7%A0%81%E5%92%8C%E8%A7%A3%E7%A0%81

# 把字段中文编码后转成 name=%E5%88%98%E6%B8%85%E6%94%BF&age=18
from urllib.parse import urlencode
d={'name':'刘清政','age':18}
res=urlencode(d)
print(res)

# 只想单独对中文编码和解码
from urllib.parse import quote, unquote

# 编码
name='刘清政'
res=quote(name)
print(res)

# 解码
s='python%20url%E7%BC%96%E7%A0%81%E5%92%8C%E8%A7%A3%E7%A0%81'
print(unquote(s))

携带请求头

# 3 携带请求头
携带请求头-->重要的key:
User-Agent:客户端浏览器的类型版本信息,操作系统版本,django中如何取出请求头--》META---》中间件--》存到数据库--》饼形图--》统计你们网站近一个月客户端类型
referer:图片防盗链 Referer: https://www.lagou.com/wn/zhaopin
记录的是上一个访问的地址---》反扒:如果上一个访问的地址不是自己的地址,认为不是正常请求就禁止


header = {
     'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36',
     'Accept-Encoding': 'gzip, deflate, br',
}
res = requests.get('https://dig.chouti.com/',headers=header)
with open('chouti.html','wb') as f:
    f.write(res.content)
print(res.text)

携带cookie

## 本身cookie是请求头中的值,那么就可以执行放在请求头中,但是cookie经常用,也可以单独是一个参数
# 模拟点赞
header = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36',
    # 'Cookie': ""
}
data={
    'linkId': '34934736'
}
# res = requests.post('https://dig.chouti.com/link/vote',data=data,headers=header)
res = requests.post('https://dig.chouti.com/link/vote',data=data,headers=header,cookies={'key':'value'})
print(res.text)

发送post请求模拟登陆

#5  模拟登陆某网站
data = {
    'username': '616564099@qq.com',
    'password': 'lqz123',
    'captcha': 'kyca',
    '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就可以模拟登陆后操作

res2=requests.get('http://www.aa7a.cn/',cookies=res.cookies)
print('616564099@qq.com' in res2.text)

每次都要手动携带cookie,麻烦,使用requests提供的session 方法

session=requests.session()
以后需所有请求都用session对象发送,不需要手动处理cookie
data = {
    'username': 'ee@qq.com',
    'password': 'lqz123',
    'captcha': 'kyca',
    'remember': 1,
    'ref': 'http://www.aa7a.cn/',
    'act': 'act_login',
}
res = session.post('http://www.aa7a.cn/user.php',data=data)
print(res.text)
print(res.cookies) # 登陆成功返回的cookie,这个cookie是登陆过后的,以后拿着这个cookie就可以模拟登陆后操作

res2=session.get('http://www.aa7a.cn/')
print('616564099@qq.com' in res2.text)

响应对象

## 6 响应对象
import requests
respone=requests.get('http://www.jianshu.com')
# respone属性
print(respone.text)  # 返回响应体的文本内容
print(respone.content)# 返回响应体的二进制内容

print(respone.status_code)# 响应状态码
print(respone.headers)# 响应头
print(respone.cookies)# 响应的cookie
print(respone.cookies.get_dict())# 响应的cookie转成字典
print(respone.cookies.items())

print(respone.url) # 请求地址
print(respone.history) # 了解---》如果有重定向,列表,放着重定向之前的地址

print(respone.encoding) # 页面的编码方式:utf-8   gbk
# response.iter_content()  # content迭代取出content二进制内容,一般用它存文件

编码问题(一般不会有问题)

# 请求回来的数据,res.text 打印的时候乱码 ,因为没有指定编码,默认已utf-8编码
# 解决
response.encoding='gbk'

获取二进制数据

# 下载图片,视频
# 下载图片
header={
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36'
}

res=requests.get('https://tva1.sinaimg.cn/mw2000/9d52c073gy1h1v6lmny8nj20j60pjtdh.jpg',headers=header)
with open('mv.jpg','wb') as f:
    # f.write(res.content)
    for line in res.iter_content(100):
        f.write(line)

解析json

## 8 json格式解析
import json

data = {
    'cname': '',
    'pid': '',
    'keyword': '上海',
    'pageIndex': 1,
    'pageSize': 10,
}
# res = requests.post('http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword',data=data)
# j = json.loads(res.text)
# print(j['Table'][0]['rowcount'])

res= requests.post('http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword',data=data).json()
print(res['Table'][0]['rowcount'])

ssl认证

# 之前网站,有些没有认证过的ssl证书,我们访问需要手动携带证书
# 跳过证书直接访问
import requests
respone=requests.get('https://www.12306.cn',verify=False) #不验证证书,报警告,返回200
print(respone.status_code)
# 手动携带
import requests
respone=requests.get('https://www.12306.cn',
                     cert=('/path/server.crt',
                           '/path/key'))
print(respone.status_code)

12 使用代理

# 爬虫,速度很快,超过频率限制---》使用代理,切换ip---》封ip封的也是代理的ip,我的ip没问题

网上有一个python开源的代理池---》自己搭建起来https://github.com/jhao104/proxy_pool
写个django,用户访问就返回用户的ip地址--》放到公网上,用manage.py跑起来---》访问
# 客户端使用request加代理访问你的django,验证代理是否使用成功

proxies = {
    'http': '39.103.217.44:59886', # http或https代理
}
respone=requests.get('https://www.baidu.com',proxies=proxies)

print(respone.status_code)

标签:respone,res,爬虫,data,print,requests,com
来源: https://www.cnblogs.com/elijah-li/p/16399105.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有