昨日回顾

1 上线(私有云,公有云)
2 虚拟化:kvm,vmware,openstack,飞天(python写的,管理虚拟机),docker,k8s
3 mysql,redis(rabbimq,mongodb,fastdfs):docekr----》拉起来就ok
4 python环境(3.6----》uwsgi(真实环境)----》虚拟环境
5 uwsgi是一个符合wsgi协议的web服务器----》负责接收http请求--》转成字典,响应对象
6 前端项目部署---》build---》css,html,js---》放在了服务器某个位置--》使用nginx转发
7 在虚拟环境中安装依赖(wheel文件安装:版本对应,搜索解决)
8 无网络的机器上安装python模块(在一个安装好的机器上导出---》copy到新机器上安装)
9 数据库创建用户,创建库
10 mysqlclient:不同平台安装,mac下需要安装connector-c
	- brew install connector-c
11 uwsgi跑django----》先用manage_pro.py跑跑试试
    <uwsgi>    
       <socket>127.0.0.1:8808</socket> <!-- 内部端口,自定义 --> 
       <chdir>/home/project/luffyapi/</chdir> <!-- 项目路径 -->            
       <module>lufeiapi.wsgi</module>  <!-- luffyapi为wsgi.py所在目录名--> 
       <processes>4</processes> <!-- 进程数 -->     
       <daemonize>uwsgi.log</daemonize> <!-- 日志文件 -->
    </uwsgi>
    
    
12 nginx 做动静分离
	收集xadmin静态文件:
    	STATIC_URL = '/static/'
		STATIC_ROOT = '/home/project/lufeiapi/lufeiapi/static'  
		STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
    	-python /home/project/lufeiapi/manage_prod.py collectstatic

     server {
            listen 8000;
            server_name  127.0.0.1; # 改为自己的域名,没域名修改为127.0.0.1:80
            charset utf-8;
            location / {
               include uwsgi_params;
               uwsgi_pass 127.0.0.1:8808;  # 端口要和uwsgi里配置的一样
               uwsgi_param UWSGI_SCRIPT lufeiapi.wsgi;  #wsgi.py所在的目录名+.wsgi
               uwsgi_param UWSGI_CHDIR /home/project/lufeiapi/; # 项目路径
            }
            # 新增的配置静态文件
            location /static {
                alias /home/project/lufeiapi/lufeiapi/static;
            }
        }
    
13 域名---》转发

今日内容

1 爬虫基本原理

1 百度是个大爬虫
2 模拟浏览器发送http请求---(请求库)(频率,cookie,浏览器头,js反扒,app逆向)(抓包工具)---从服务器取回数据---解析数据---(解析库)(反扒)---入库(存储库)
3 爬虫协议

2 requests模块

0 urllib 内置库,发送http请求,比较难用,requests是基于这个库的写的
1 requests,应用非常广泛的请求库
2 request-html库(request,bs4,lxml等二次封装)

3 User-Agent:请求头中标志是什么客户端发送的请求
4 Referer:上次请求的地址

2.1 发生post请求

携带数据,携带头,携带cookie

import requests
# res=requests.get('https://www.cnblogs.com/')
# # print(res.text)  # 文本内容
# print(res.content) # 二进制内容 视频,图片  res.iter_content()


## 携带参数
# header={
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36'
# }
#https://www.sogou.com/web?query=%E7%87%95%E7%AA%9D
# # res=requests.get('https://www.baidu.com/s?wd=帅哥',headers=header)
# res=requests.get('https://www.baidu.com/s',headers=header,params={'wd':'老奶奶'})
#
# with open('baidu.html','wb') as f:
#     f.write(res.content)
# # print(res.text)


#### url编码和解码
from urllib.parse import urlencode,unquote
# wd='燕窝'
# encode_res=urlencode({'k':wd},encoding='utf-8')
# print(encode_res)

res=unquote('%E7%87%95%E7%AA%9D')
print(res)


### 携带头
'''
Host  :
Referer :大型网站通常都会根据该参数判断请求的来源
User-Agent: 客户端
Cookie :Cookie信息虽然包含在请求头里,但requests模块有单独的参数来处理他,headers={}内就不要放它了
# cookie携带的两种方式,如果是字符串形式,就放在请求头中
如果是字典,cookiejar的对象,就放在参数中cookies=字典,cookiejar的对象
'''

# header={
#     'Cookie':'BIDUPSID=BAA91359911D4514C92ABD22B88961AE; PSTM=1601026753; MCITY=-289%3A; BD_UPN=12314753; sug=3; sugstore=0; ORIGIN=0; bdime=0; BAIDUID=EA290B9A7A371E91762170324A4ED8C0:FG=1; BDSFRCVID=R60OJexroG3VeHne34XvvrEMkvqMFyTTDYLEJs2qYShnrsPVJeC6EG0PtoWQkz--EHtdogKK0gOTH6KF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; Jj4BC3MahvKQDOxsoyKt6lQe_JLqTN3KJmfKn1bUbA5DrXWxvM2'
# }
res = requests.get('https://www.baidu.com/',cookies='',headers=header)

2.2 发送post请求

携带数据, 携带头,携带cookie

import requests


## post请求携带数据
# res=requests.post('地址',headers=字典,cookie=对象,params='放在链接中',
#                   data='字典,放在请求体中',json='json格式字符串,放到请求体中')

''''
#如果我们自定义请求头是application/json,并且用data传值, 则服务端取不到值
requests.post(url='',
              data={'':1,},
              headers={
                  'content-type':'application/json'
              })

requests.post(url='',
              json={'':1,},
              ) #默认的请求头:application/json
'''


## session对象,自动处理cookie,不需要人为处理cookie
## 一旦登录成功,以后,不需要手动携带cookie了,直接使用session对象发送请求,会自动携带


# session=requests.session()
#
data = {
     'username': '616564099@qq.com',
     'password': 'lqz123',
     'captcha': '123',
     'remember': 1,
     'ref': 'http://www.aa7a.cn/',
     'act': 'act_login',
 }
 header = {
     'Referer': 'http://www.aa7a.cn/user.php?&ref=http%3A%2F%2Fwww.aa7a.cn%2Fuser.php%3Fact%3Dlogout',
     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36'
 }
res = session.post('http://www.aa7a.cn/user.php', headers=header,data=data)
#
#
res=session.get('http://www.aa7a.cn/')
print('616564099@qq.com' in res.text)

2.3 高级用法

使用代理,上传文件,超时设置

import requests
### SSL Cert Verification(了解即可)

## 方式一
# respone=requests.get('https://www.12306.cn',verify=False) #不验证证书,报警告,返回200
#
# # 手动携带证书
import requests
respone=requests.get('https://www.12306.cn',
                      cert=('/path/server.crt',
                            '/path/key'))
print(respone.status_code)


#超时设置
import requests
respone=requests.get('https://www.baidu.com',
                     timeout=0.01)
print(respone.text)

# 认证设置(了解)
# import requests
# from requests.auth import HTTPBasicAuth
# r=requests.get('xxx',auth=HTTPBasicAuth('user','password'))
# print(r.status_code)


# 异常处理
import requests
from requests.exceptions import * #可以查看requests.exceptions获取异常类型
#
try:
    r=requests.get('http://www.baidu.com',timeout=0.00001)
# # except ReadTimeout:
# #     print('===:')
# # except ConnectionError: #网络不通
# #     print('-----')
# # except Timeout:
# #     print('aaaaa')
#
except Exception as e:
    print(e)



## 上传文件
import requests
files={'file':open('a.jpg','rb')}
respone=requests.post('http://httpbin.org/post',files=files)
print(respone.status_code)


## 代理设置

import requests
# 免费代理(不稳定)
# 收费代理(稳定)
proxies={
    'HTTP':'117.95.200.239:9999',
}
respone=requests.get('https://www.12306.cn',
                     proxies=proxies)

print(respone.status_code)

# 写一个django,取出访问者的ip地址,使用requests,加代理模块访问

# 高匿和透明
# 高匿:服务端取不到真实的ip
# 透明:服务端可以取到真实的ip地址  请求头中:X-Forwarded-For   Meta

# 代理池:搞一堆代理,放到列表中,每次发请求,随机出一个(开源代理池)

2.4 响应对象的方法

import requests
respone=requests.get('https://www.autohome.com.cn/shanghai/')
# respone属性
print(respone.text)  # 文本内容
print(respone.content)  # 二进制

print(respone.status_code)  # 状态码
print(respone.headers)  # 响应头
print(type(respone.cookies))  # cookie对象  RequestsCookieJar
from requests.cookies import RequestsCookieJar
print(respone.cookies.get_dict()) # cookie对象转成字典
print(respone.cookies.items())



print(respone.url)    # 请求地址
print(respone.history) # 当你访问一个网站,有30x,重定向之前的地址,

print(respone.encoding) # 网站编码

respone.encoding='gb2312'
print(respone.text)

#关闭:response.close()
from contextlib import closing
with closing(requests.get('xxx',stream=True)) as response:
    for line in response.iter_content():
    pass

## 解析json


import json
json.loads(respone.text)
respone.json()

4 自动登录某网站


import requests
# res=requests.get('http://www.aa7a.cn/user.php?&ref=http%3A%2F%2Fwww.aa7a.cn%2F')
# cookie=res.cookies
data = {
    'username': '616564099@qq.com',
    'password': 'lqz123',
    'captcha': '123',
    'remember': 1,
    'ref': 'http://www.aa7a.cn/',
    'act': 'act_login',
}
header = {
    'Referer': 'http://www.aa7a.cn/user.php?&ref=http%3A%2F%2Fwww.aa7a.cn%2Fuser.php%3Fact%3Dlogout',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36'
}
res = requests.post('http://www.aa7a.cn/user.php', headers=header,data=data)
# print(res.text)
cookie=res.cookies  # 登录成功拿到cookie----》模拟下单了

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


作业

1 爬好看视频
2 自动给抽屉评论和点赞
3 django测试代理ip的使用

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐