day36 爬虫+http请求+高性能
爬虫
参考博客:http://www.cnblogs.com/wupeiqi/articles/5354900.html
http://www.cnblogs.com/wupeiqi/articles/6283017.html
- 基本操作
概要:
- 发送Http请求,Python Http请求,requests
- 提取指定信息,Python 正则表达式,beautifulsoup
- 数据持久化,
Python两个模块
- requests
- beautifulsoup
Http请求相关知识
- 请求:
请求头:
- cookie
请求体:
- 发送内容
- 响应:
响应头
- 浏览器读取
响应体
- 看到的内容
特殊:
- cookie
- csrftoken
- content-type:
content-type:application/url-form....
name=alex&age=18
content-type:application/json
{name:'alex',age:18}
- 性能相关
- 串行: 1个人,一个任务一个任务,空余时间,玩。
- 线程: 10个人,一个任务一个任务,空余时间,玩。
- 进程: 10个家庭,一个任务一个任务,空余时间,玩。
- 【协程】异步非阻塞:1个人,充分利用时间。
- scrapy框架
- 规则
- redis-scrapy组件
内容详细:
- 基本操作,python伪造浏览器发送请求并或者指定内容
pip3 install requests
response = requests.get('http://www.baidu.com')
response.text
pip3 install beautifulsoup4
from bs4 import Beautifulsoup
soup = Beautifulsoup(response.text,'html.parser')
soup.find(name='h3',attrs={'class':'t'})
soup.find_all(name='h3')
示例:爬取汽车之家新闻
- 模块
requests
GET:
requests.get(url="http://www.oldboyedu.com")
# data="http GET / http1.1\r\nhost:oldboyedu.com\r\n....\r\n\r\n"
requests.get(url="http://www.oldboyedu.com/index.html?p=1")
# data="http GET /index.html?p=1 http1.1\r\nhost:oldboyedu.com\r\n....\r\n\r\n"
requests.get(url="http://www.oldboyedu.com/index.html",params={'p':1})
# data="http GET /index.html?p=1 http1.1\r\nhost:oldboyedu.com\r\n....\r\n\r\n"
POST:
requests.post(url="http://www.oldboyedu.com",data={'name':'alex','age':18}) # 默认请求头:url-formend....
data="http POST / http1.1\r\nhost:oldboyedu.com\r\n....\r\n\r\nname=alex&age=18"
requests.post(url="http://www.oldboyedu.com",json={'name':'alex','age':18}) # 默认请求头:application/json
data="http POST / http1.1\r\nhost:oldboyedu.com\r\n....\r\n\r\n{"name": "alex", "age": 18}"
requests.post(
url="http://www.oldboyedu.com",
params={'p':1},
json={'name':'alex','age':18}
) # 默认请求头:application/json
data="http POST /?p=1 http1.1\r\nhost:oldboyedu.com\r\n....\r\n\r\n{"name": "alex", "age": 18}"
补充:
request.body,永远有值
request.POST,可能没有值
beautifulsoup
soup = beautifulsoup('HTML格式字符串','html.parser')
tag = soup.find(name='div',attrs={})
tags = soup.find_all(name='div',attrs={})
tag.find('h3').text
tag.find('h3').get('属性名称')
tag.find('h3').attrs
HTTP请求:
GET请求:
data="http GET /index?page=1 http1.1\r\nhost:baidu.com\r\n....\r\n\r\n"
POST请求:
data="http POST /index?page=1 http1.1\r\nhost:baidu.com\r\n....\r\n\r\nname=alex&age=18"
socket.sendall(data)
示例【github和抽屉】:任何一个不用验证码的网站,通过代码自动登录
1. 按理说
r1 = requests.get(url='https://github.com/login')
s1 = beautifulsoup(r1.text,'html.parser')
val = s1.find(attrs={'name':'authenticity_token'}).get('value')
r2 = requests.post(
url= 'https://github.com/session',
data={
'commit': 'Sign in',
'utf8': '✓',
'authenticity_token': val,
'login':'xxxxx',
'password': 'xxxx',
}
)
r2_cookie_dict = r2.cookies.get_dict() # {'session_id':'asdfasdfksdfoiuljksdf'}
保存登录状态,查看任意URL
r3 = requests.get(
url='xxxxxxxx',
cookies=r2_cookie_dict
)
print(r3.text) # 登录成功之后,可以查看的页面
2. 不按理说
r1 = requests.get(url='https://github.com/login')
s1 = beautifulsoup(r1.text,'html.parser')
val = s1.find(attrs={'name':'authenticity_token'}).get('value')
# cookie返回给你
r1_cookie_dict = r1.cookies.get_dict()
r2 = requests.post(
url= 'https://github.com/session',
data={
'commit': 'Sign in',
'utf8': '✓',
'authenticity_token': val,
'login':'xxxxx',
'password': 'xxxx',
},
cookies=r1_cookie_dict
)
# 授权
r2_cookie_dict = r2.cookies.get_dict() # {}
保存登录状态,查看任意URL
r3 = requests.get(
url='xxxxxxxx',
cookies=r1_cookie_dict
)
print(r3.text) # 登录成功之后,可以查看的页面
- requests
"""
1. method
2. url
3. params
4. data
5. json
6. headers
7. cookies
8. files
9. auth
10. timeout
11. allow_redirects
12. proxies
13. stream
14. cert
================ session,保存请求相关信息(不推荐)===================
import requests
session = requests.Session()
i1 = session.get(url="http://dig.chouti.com/help/service")
i2 = session.post(
url="http://dig.chouti.com/login",
data={
'phone': "8615131255089",
'password': "xxooxxoo",
'oneMonth': ""
}
)
i3 = session.post(
url="http://dig.chouti.com/link/vote?linksId=8589523"
)
print(i3.text)
"""
- beautifulsoup
- find()
- find_all()
- get()
- attrs
- text
内容:
1. 示例:汽车之家
2. 示例:github和chouti
3. requests和beautifulsoup
4. 轮询和长轮询
5. Django
request.POST
request.body
# content-type:xxxx
作业:web微信
功能:
1. 二维码显示
2. 长轮询:check_login
3.
- 检测是否已经扫码
- 扫码之后201,头像: base64:.....
- 点击确认200,response.text redirect_ur=....
4. 可选,获取最近联系人信息
安装:
twsited
scrapy框架
课堂代码:https://github.com/liyongsan/git_class/tree/master/day36
day36 爬虫+http请求+高性能的更多相关文章
- Python3 网络爬虫(请求库的安装)
Python3 网络爬虫(请求库的安装) 爬虫可以简单分为几步:抓取页面,分析页面和存储数据 在页面爬取的过程中我们需要模拟浏览器向服务器发送请求,所以需要用到一些python库来实现HTTP的请求操 ...
- 爬虫 Http请求,urllib2获取数据,第三方库requests获取数据,BeautifulSoup处理数据,使用Chrome浏览器开发者工具显示检查网页源代码,json模块的dumps,loads,dump,load方法介绍
爬虫 Http请求,urllib2获取数据,第三方库requests获取数据,BeautifulSoup处理数据,使用Chrome浏览器开发者工具显示检查网页源代码,json模块的dumps,load ...
- HTTP请求中的User-Agent 判断浏览器类型的各种方法 网络爬虫的请求标示
我们知道,当用户发送一个http请求的时候,浏览的的版本信息也包含在了http请求信息中: 如上图所示,请求 google plus 请求头就包含了用户的浏览器信息: User-Agent:Mozil ...
- fake-useragent,python爬虫伪装请求头
在编写爬虫进行网页数据的时候,大多数情况下,需要在请求是增加请求头,下面介绍一个python下非常好用的伪装请求头的库:fake-useragent,具体使用说明如下: 1.在scrapy中的使用 第 ...
- 爬虫、请求库selenium
阅读目录 一 介绍 二 安装 三 基本使用 四 选择器 五 等待元素被加载 六 元素交互操作 七 其他 八 项目练习 一 介绍 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决r ...
- 爬虫、请求库requests
阅读目录 一 介绍 二 基于GET请求 三 基于POST请求 四 响应Response 五 高级用法 一 介绍 #介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,req ...
- python爬虫#网络请求requests库
中文文档 http://docs.python-requests.org/zh_CN/latest/user/quickstart.html requests库 虽然Python的标准库中 urlli ...
- python 爬虫001-http请求过程
HTTP 请求流程 一次完整的HTTP请求过程从TCP三次握手建立连接成功后开始,客户端按照指定的格式开始向服务端发送HTTP请求,服务端接收请求后,解析HTTP请求,处理完业务逻辑,最后返回一个HT ...
- python爬虫添加请求头和请求主体
添加头部信息有两种方法 1.通过添加urllib.request.Request中的headers参数 #先把要用到的信息放到一个字典中 headers = {} headers['User-Agen ...
随机推荐
- python介绍和基础(待补充)
python的介绍 把命令放到一个文件中,文件还能执行,这样的语言叫shell脚本 写一个c语言程序,.c结尾的,gcc运行c语言程序,生成.out文件,然后执行.out文件 c语言是先编写代码,再编 ...
- 转:WebClient类(温习一下)
WebClient类提供向 URI 标识的资源发送数据和从 URI 标识的资源接收数据的公共方法. 其实就相当于创建一个请求客户端.可以获取网页和各种各样的信息,包括交互. 通过MSDN来看看WebC ...
- 84. Largest Rectangle in Histogram(直方图最大面积 hard)
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- Problem A. Array Factory XVII Open Cup named after E.V. Pankratiev Stage 4: Grand Prix of SPb, Sunday, Octorber 9, 2016
思路: 直接二分长度不可行,因为有负数. 考虑枚举坐便删l个数,那如果可以在短时间内求出符合条件的右边最小删的数的个数,这题便可做了. 即:当左边删l个数时,要使sum[n]-sum[l]-fsum[ ...
- Spring-1-E Game(HDU 5011)解题报告及测试数据
Game Time Limit:1000MS Memory Limit:65536KB Description Here is a game for two players. The rule ...
- Jmeter使用流程及简单分析监控(转载)
转载自:https://www.cnblogs.com/linglingyuese/archive/2013/03/04/linglingyuese-one.html#undefined 一.安装Jm ...
- tomcat结合memcached构建session服务器
memcached服务器两台:192.168.223.136,192.168.223.137 tomcat多实例:192.168.233.146:8081,192.168.223.146:8082 f ...
- vsftpd搭建ftp服务,并实现虚拟用户访问
安装vsftpd服务: yum install vsftpd -y [root@wadeson ~]# rpm -ql vsftpd /etc/logrotate.d/vsftpd /etc/pam. ...
- DDR4中的so-dimm 和component
so-dimm :Small Outline Dual In-line Memory Module (小型双列直插式内存模块) component:直接焊接的ddr4芯片
- linux中断的下半部机制
一.中断处理为什么要下半部?Linux在中断处理中间中断处理分了上半部和下半部,目的就是提高系统的响应能力和并发能力.通俗一点来讲:当一个中断产生,调用该中断对应的处理程序(上半部)然后告诉系统,对应 ...