回顾:http协议基于请求响应的方式,请求:请求首行 请求头{'keys':vales} 请求体 ;响应:响应首行,响应头{'keys':'vales'},响应体。

import socket

sock=socket.socket()
sock.bind(("127.0.0.1",8808))
sock.listen(5) while 1:
print("server waiting.....")
conn,addr=sock.accept()
data=conn.recv(1024)
print("data", data) # 读取html文件
with open("login.html","rb") as f:
data=f.read() conn.send((b"HTTP/1.1 200 OK\r\nContent-type:text/html\r\n\r\n%s"%data))
conn.close()

基于socket的浏览器交互

'''
GET请求
# 请求首行
GET / HTTP/1.1\r\n
# get请求后面的参数
b'GET /?name=wd&age=11 HTTP/1.1\r\n
# 请求头
Host: 127.0.0.1:8008\r\n
Connection: keep-alive\r\n
Cache-Control: max-age=0\r\n
Upgrade-Insecure-Requests: 1\r\n
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181Safari/537.36\r\n
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\r\nAccept-Encoding: gzip, deflate, br\r\n
Accept-Language: zh-CN,zh;q=0.9\r\n Cookie:csrftoken=7xx6BxQDJ6KB0PM7qS8uTA892ACtooNbnnF4LDwlYk1Y7S7nTS81FBqwruizHsxF\r\n\r\n'
# 请求体(get请求,请求体为空)
'''
b''
'''
POST请求
# 请求首行
b'POST /?name=wd&age=11 HTTP/1.1\r\n
# 请求头
Host: 127.0.0.1:8008\r\n
Connection: keep-alive\r\n
Content-Length: 21\r\n
Cache-Control: max-age=0\r\n
Origin: http://127.0.0.1:8008\r\n
Upgrade-Insecure-Requests: 1\r\n
Content-Type: application/x-www-form-urlencoded\r\n
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36\r\n
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\r\n
Referer: http://127.0.0.1:8008/?name=lqz&age=18\r\n
Accept-Encoding: gzip, deflate, br\r\n
Accept-Language: zh-CN,zh;q=0.9\r\n
Cookie:csrftoken=7xx6BxQDJ6KB0PM7qS8uTA892ACtooNbnnF4LDwlYk1Y7S7nTS81FBqwruizHsxF\r\n\r\n'
# 请求体
b'name=wd&password=11' '''

请求

b"HTTP/1.1 200 OK\r\n
Content-type:text/html\r\n\r\n
%s"%data

响应

http原理

点击详情

梨视频案例

#返回数据3种格式
#1.text 匹配需要的东西
#2.content(二进制) 保存成图片,视频等
#3.json 反序列化成字典或列表 #下载功能
def download(videos,title):
if not os.path.exists('video'):
os.mkdir('video')
path=os.path.join('video',title)+'.mp4'
res=requests.get(videos)
with open(path,'wb') as f:
f.write(res.content) #起线程执行执行
if __name__ == '__main__':
from concurrent.futures import ThreadPoolExecutor
p=ThreadPoolExecutor(10)
for i in parser_index(get_index()):
dic=video_info(get_video(i))
print(dic)
p.submit(download,dic['video'],dic['title'])
p.shutdown(wait=True)

#注意问题:梨视频下滑加载视频(是根据url的参数,例如分类下的视频显示多少)

github登陆实例

#get请求登陆页面 获取csrf随机字符串和cookies

#post请求登陆操作 携带csrf,输入的用户名密码等(请求体数据) 和 cookies,user-agent,referer等(请求头数据) 必须数据

数据是请求体还是请求头数据? (我的理解是比如ajax里的data,django的返回数据都是请求体的数据. request.set_cookies('islogin':'true') request对象的数据为请求头的)

"""
1.请求登陆页面 获取token cookie
2.发生登陆的post请求,将用户名密码 和token 放在请求体中,cookie放在请求头中 """
import requests
import re
login_url = "https://github.com/login"
#浏览器标识
headers = {"user-agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"}
#请求登陆页面
res1 = requests.get(login_url,headers=headers) print(res1.status_code)
# 从响应体中获取token
token = re.search('name="authenticity_token" value="(.*?)"',res1.text).group(1) # 保存cookie
login_cookie = res1.cookies.get_dict()
print(login_cookie) # 发送登陆请求
res2 = requests.post("https://github.com/session",
headers={
"user-agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"},
cookies = login_cookie,
data={
"commit": "Sign in",
"utf8": "✓",
"authenticity_token": token,
"login": "xxxxxxxxxxx",
"password": "xxxxxxxxxxx"},
# 是否允许自动重定向
allow_redirects = False)
print(res2.status_code) # 用户登录成功后的cookie
user_cookie = res2.cookies.get_dict() # 携带用户cookies访问主页
res3 = requests.get("https://github.com/settings/profile",cookies = user_cookie,headers = headers)
print(res3.status_code)
print(res3.text)
# "https://github.com/settings/profile"

requests请求参数小总结

#get请求参数
kwd = "吴秀波出轨门"
url = "https://www.baidu.com/s"
requests.get(url,headers=headers,params={"wd":kwd}) #post请求参数
requests.post("https://github.com/session",
headers={
"user-agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"},
cookies = login_cookie,
data={
"commit": "Sign in",
"utf8": "✓",
"authenticity_token": token,
"login": "ssssss",
"password": "ssssss"},
# 是否允许自动重定向
allow_redirects = False)
#返回值处理
# response.cookies.get_dict() #获取cookies
# response.status_code # 状态码
# response.text # 将结果以文本的形式返回
# response.content # 将结果以二进制的方式返回
# response.json() # 将数据直接反序列化得到字典或是列表

主要代码内容

爬虫 http原理,梨视频,github登陆实例,requests请求参数小总结的更多相关文章

  1. HFun.快速开发平台(二)=》自定义列表实例(请求参数的处理)

    上编描述了自定义列表的基本实现功能,本此记录列表的请求过程. 个人比较喜欢对参数进行对象化,方便后续人维护及查看,先上代码: /************************************ ...

  2. HFun.快速开发平台(四)=》自定义列表实例(请求参数的处理)

    上编自定义列表描述了自定义列表的基本实现功能,本此记录列表的请求过程. 个人比较喜欢对参数进行对象化,方便后续人维护及查看,先上代码: /******************************* ...

  3. 基础爬虫,谁学谁会,用requests、正则表达式爬取豆瓣Top250电影数据!

    爬取豆瓣Top250电影的评分.海报.影评等数据!   本项目是爬虫中最基础的,最简单的一例: 后面会有利用爬虫框架来完成更高级.自动化的爬虫程序.   此项目过程是运用requests请求库来获取h ...

  4. 爬虫 requests模块的其他用法 抽屉网线程池回调爬取+保存实例,gihub登陆实例

    requests模块的其他用法 #通常我们在发送请求时都需要带上请求头,请求头是将自身伪装成浏览器的关键,常见的有用的请求头如下 Host Referer #大型网站通常都会根据该参数判断请求的来源 ...

  5. python爬虫实践——爬取“梨视频”

    一.爬虫的基本过程: 1.发送请求(请求库:request,selenium) 2.获取响应数据()服务器返回 3.解析并提取数据(解析库:re,BeautifulSoup,Xpath) 4.保存数据 ...

  6. 开源磁力搜索爬虫dhtspider原理解析

    开源地址:https://github.com/callmelanmao/dhtspider. 开源的dht爬虫已经有很多了,有php版本的,python版本的和nodejs版本.经过一些测试,发现还 ...

  7. Python 爬虫——抖音App视频抓包

    APP抓包 前面我们了解了一些关于 Python 爬虫的知识,不过都是基于 PC 端浏览器网页中的内容进行爬取.现在手机 App 用的越来越多,而且很多也没有网页端,比如抖音就没有网页版,那么上面的视 ...

  8. Python 爬虫的工具列表 附Github代码下载链接

    Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...

  9. 开源大数据技术专场(下午):Databircks、Intel、阿里、梨视频的技术实践

    摘要: 本论坛第一次聚集阿里Hadoop.Spark.Hbase.Jtorm各领域的技术专家,讲述Hadoop生态的过去现在未来及阿里在Hadoop大生态领域的实践与探索. 开源大数据技术专场下午场在 ...

随机推荐

  1. 纯手工搭建VS 2017(社区 免费版)离线安装包

    不知不觉中,史上功能最强大的Visual Studio 2017版本发于美国时间2017年3月8日正式在发布了,但是由于版本更新速度加快和与第三方工具包集成的原因,微软研发团队没有为这个版本提供离线下 ...

  2. (五)jdk8学习心得之默认方法

    五.默认方法 1. 使用方法:写在接口中,就是为了接口可以做一些事情. 2. 目的:有很多实现类,有一个公共的抽象方法,其实这些实现类实现该抽象方法的内容是完全一致的,完全没有必要都重新实现一遍.并且 ...

  3. 01——Solr学习之全文检索服务系统的基础认识

    一.为什么要用Solr,Solr是个什么东西? 1.1.Solr是个开源的搜索服务器 1.2.我们用Solr主要实现搜索功能,一般的网站首页都会有一个大大的搜索框,用来搜索此网站上的商品啊什么的,如下 ...

  4. ubuntu apt-get install 时报错curl : Depends: libcurl4 (= 7.58.0-2ubuntu3.6) but 7.61.0-1ubuntu2 is to be installed或者 vim : Depends: vim-common (= 2:8.0.1453-1ubuntu1) but 2:8.0.1766-1ubuntu1 is to be ins

    ubuntu apt-get install 时报错:Depends: ***(=某版本)but***(另一版本)is to be installed 这时候就把这个***给purge后再重新装就好了 ...

  5. Docker常用镜像

    Docker,具有快捷方便的特性,机器上不需要安装软件和进行各种配置,拉取镜像,一行命令即可启动服务,不使用时,一行命令关闭容器即可,快捷方便,干净.利索.建议将本地的redis.mysql.kafk ...

  6. 【长期更新】迈向现代化的 .Net 配置指北

    1. 欢呼 .NET Standard 时代 我现在已不大提 .Net Core,对于我来说,未来的开发将是基于 .NET Standard,不仅仅是 面向未来 ,也是 面向过去:不只是 .Net C ...

  7. python3.7安装pylint

    python3.7安装pylint之"setuptools"版本错误 借鉴:错题集(已解决):pyinstaller报错ModuleNotFoundError: No module ...

  8. ECMA262,JavaScript引擎,浏览器

    相关阅读:https://www.cnblogs.com/970119449blog/p/8080133.html 相关阅读:https://www.jb51.net/article/75888.ht ...

  9. luogu4365 秘密袭击 (生成函数+线段树合并+拉格朗日插值)

    求所有可能联通块的第k大值的和,考虑枚举这个值: $ans=\sum\limits_{i=1}^{W}{i\sum\limits_{S}{[i是第K大]}}$ 设cnt[i]为连通块中值>=i的 ...

  10. spring boot下使用logback或log4j生成符合Logstash标准的JSON格式

    spring boot下使用logback或log4j生成符合Logstash标准的JSON格式 一.依赖 由于配置中使用了json格式的日志输出,所以需要引入如下依赖 "net.logst ...