【Python学习】request库

Requests库(https://www.python-requests.org/)是一个擅长处理那些复杂的HTTP请求、cookie、header(响应头和请求头)等内容的Python第三方库

提交一个最基本的表单

大多数网页表单都是由一些HTML字段、一个提交按钮、一个在表单处理完之后跳转的“执行结果”(表单属性action的值)页面构成。

一个最简单的表单(http://www.pythonscraping.com/pages/files/form.html)

这个表单的源码在下面。可以通过chrome的开发者工具(F12)查看。

<form method="post" action="processing.php">
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname"><br>
<input type="submit" value="Submit" id="submit">
</form>

有几个要点:

  • 两个要输入字段的名称是firstname和lastname。字段的名称决定了表单被确认后要被传送到服务器上的变量名称,要模拟表单提交数据的行为,就要保证变量名称与字段名称是一一对应的。
  • 表单的真实行为其实发生在processing.php(绝对路径是http://www.pythonscraping.com/pages/files/processing.php)。表单的任何POST请求其实都发生在这个页面上,并非表单本身所在的页面。HTML表单的目的,知识帮助网站的访问者发送格式合理的请求,向服务器请求没有出现的页面。

那么提交这个最简单的表单,只要四行代码就可以了。

import requests

params = {‘firstname‘: ‘Ivy‘, ‘lastname‘: ‘Wong‘}
r= requests.post("http://www.pythonscraping.com/pages/files/processing.php", data=params)
print(r.text)

表单提交后,程序应该会返回执行页面的源代码,包括这行内容。

提交文件和图像

在http://www.pythonscraping.com/files/form2.html有一个文件上传表单,表单的源代码是下面这样的。

<form action="../pages/files/processing2.php" method="post" enctype="multipart/form-data">
  Submit a jpg, png, or gif: <input type="file" name="uploadFile"><br>
  <input type="submit" value="Upload File">
</form>

发现input标签里有一个type属性是file,和文字其实差不多。

import requests

filess = {‘uploadFile‘: open(‘..files/Python-logo.png‘,‘rb‘)}
r= requests.post("http://www.pythonscraping.com/pages/files/processing2.php", files=files)
print(r.text)

处理登录与cookie

大多数新式的网站都用cookies跟踪用户是否已登录的状态信息。一旦网站验证了你的登录权证,它就会将它们保存在你的浏览器的cookie中,里面通常包含一个服务器生产的令牌、登录有效时限和状态跟踪信息。网站会把这个cookie当作信息验证的证据,在你浏览网站的每个页面时出示给服务器。

Ryan Mitchell在http://www.pythonscraping.com/pages/cookies/login.html创建了一个简单的登录表单。

用户名可以是任意值,但是密码必须是"password"。

这个表单在欢迎页面(http://www.pythonscraping.com/pages/cookies/welcome.php)处理,里面包含一个简介界面:http://www.pythonscraping.com/pages/cookies/profile.php

在简介页面,网站会监测浏览器的cookie,看它有没有页面已登录的设置信息。

import requests

params = {‘username‘:‘Ryan‘,‘password‘:‘password‘}
r=requests.post("http://www.pythonscraping.com/pages/cookies/welcome.php",params)
print("Cookie is set to:")
print(r.cookies.get_dict())
print("-----------------------")
print("Going to profile page...")
r=requests.get("http://www.pythonscraping.com/pages/cookies/profile.php",cookies=r.cookies)
print(r.text)

有些网站比较复杂,cookie经常暗自调整。那么可以用session函数。

import requests

session = requests.Session()

params = {‘username‘:‘Ryan‘,‘password‘:‘password‘}
r=session.post("http://www.pythonscraping.com/pages/cookies/welcome.php",params)
print("Cookie is set to:")
print(r.cookies.get_dict())
print("-----------------------")
print("Going to profile page...")
r=session.get("http://www.pythonscraping.com/pages/cookies/profile.php")
print(r.text)

会话(session)对象会持续跟踪会话信息,比如cookie、header,甚至包括运行HTTP协议的信息,比如HTTPAdapter。

修改请求头

HTTP的请求头是在你每次向网络服务器发送请求时,传递的一组属性和配置信息。HTTP定义了十几种古怪的请求头类型,不过大多数都不常用。只有下面的七个字段被大多数浏览器用来初始化所有网络请求。(表内是我的浏览器数据)

属性 内容
Host hpd.baidu.com
Connection keep-alive
Accept image/webp,image/apng,image/,/*;q=0.8
User-Agent Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36
Referrer https://www.baidu.com/
Accept-Encoding gzip, deflate, br
Accept-Language zh-CN,zh;q=0.9

而经典的Python爬虫在使用urllib标准库时,都会发送如下的请求头:

属性 内容
Accept-Encoding indentity
User-Agent Python-urllib/3.4

http://www.whatismybrowser.com/网站可以让服务器测试浏览器的属性。用下面的代码来采集这个网站的信息,验证我们浏览器的cookie设置

import requests
from bs4 import BeautifulSoup

session = requests.Session()
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36","Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"}
url="https://www.whatismybrowser.com/developers/what-http-headers-is-my-browser-sending"
req=session.get(url, headers=headers)

bsObj = BeautifulSoup(req.text, "lxml")
print(bsObj.find("table",{"class":"table-striped"}).get_text)

和Ryan给的代码稍有点不一样,加上了BeautifulSoup要用lxml解析,可能是由于我的header和Ryan不一样。

通常真正重要的参数就是User-Agent。如果在处理一个警觉性非常高的网站,就要注意那些经常用却很少检查的请求头。

请求头还可以让网站改变内容的布局样式。例如,用移动设备浏览网站时,通常会看到一个没有广告、Flash以及其他干扰的简化的网站版本。

Ryan给了一个移动设备的User-Agent如下。

User-Agent:Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, lke Gecko) Version/7.0 Mobile/11D257 Safari/95
37.53

【参考】

[1]《Python网络数据采集》Ryan Mitchell

原文地址:https://www.cnblogs.com/IvyWong/p/10093463.html

时间: 2024-08-29 13:01:06

【Python学习】request库的相关文章

在Python中用Request库模拟登录(二):博客园(简单加密,无验证码)

源代码分析 博客园的登录页面非常简单,查看网页源代码,可以发现两个输入框的id分别为input1.input2,复选框的id为remember_me,登录按钮的id为signin. 还有一段JavaScript代码,下面来简单分析一下. 先来看$(function(){});函数: 1 $(function () { 2 $('#signin').bind('click', function () { 3 signin_go(); 4 }).val('登 录'); 5 }); $(functio

[python]用request库来处理Http协议-收集北航表白墙内的数据

最近阅读了<图解Http>这本书.虽然名为"图解",可说实话里面的图真是啥用都没..不过书还是不错的,两个小时跳读完后,对Http协议有了大概的了解..对于不搞前端开发的我,这些知识应该是够用了. 继续Python折腾之旅吧! Requests is the only Non-GMO HTTP library for Python, safe for human consumption. Warning: Recreational use of other HTTP lib

[python 学习] requests 库的使用

1.get请求 # -*- coding: utf-8 -*- import requests URL_IP = "http://b.com/index.php" pyload = {'cate':1,'id':2} headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36'

在Python中用Request库模拟登录(一):字幕库(无加密,无验证码)

如此简单(不安全)的登录表单已经不多见了.字幕库的登录表单如下所示,其中省去了无关紧要的内容: 1 <form class="login-form" action="/User/login.html" method="post"> 2 <input type="hidden" name="referer" value="http://www.zimuku.net/"&g

基于python的request库,模拟登录csdn博客

以前爬虫用urllib2来实现,也用过scrapy的爬虫框架,这次试试requests,刚开始用,用起来确实比urllib2好,封装的更好一些,使用起来简单方便很多. 安装requests库     最简便的方法就是使用pip来安装:pip install requests:如果需要安装特定版本,则在后面加上版本号即可:pip install requests == 1.9.7,这样就搞定了. 快速上手的小例子 下面说一个最简单的例子: 第一行,引入requests库,这是必然的. 第二行,通过

在Python中用Request库模拟登录(四):哔哩哔哩(有加密,有验证码)

抓包分析 获取验证码 获取加密公钥 其中hash是变化的,公钥key不变 登录 其中用户名没有被加密,密码被加密. 因为在获取公钥的时候同时返回了一个hash值,推测此hash值与密码加密有关. 通过谷歌浏览器控制台分析js代码 右键登录按钮,检查,查看 Event Listeners ,点击a.btn.btn-login右边的login.4f030c3....js:6查看js代码. 点击左下角的{}展开代码 因为获取公钥和hash的链接中有action=getkey,尝试在源代码中搜索getk

python学习--标准库之os 实例(3)

#!/usr/bin/env python3 # -*- coding: utf-8 -*- #列出当前目录下文件的大小和创建日期及文件名,相当于ls -l命令 from datetime import datetime import os pwd = os.path.abspath('.') print(' Size Last Modified Name') print('------------------------------------------------------------'

python学习—turtle库练习

# coding=utf-8 import turtle # 画五角星 def drawStar(x): turtle.begin_fill() for i in range(5): turtle.forward(x) turtle.right(144) turtle.end_fill() # 转移位置 def goTo(x, y): turtle.up() turtle.goto(x, y) turtle.down() turtle.setup(864, 576) turtle.bgcolor

Python Request库学习(一)

一.安装Requests库 pip install requests 二.Request库基本使用 在Request库中常见的两种方法就是GET方法和Post方法,安装成功后,可以看到它自带了两个示例: 此外还支持De'lete.Put.Options方法.而equests库的方便之处在于所有的请求都可以使用一句代码来实现. 三.Get方法 使用Get: 简单示例: 运行结果: Params: Get方法常见的形式:https://ip:port/xxxx?xx=xx,如果访问带有参数的Url,

Python学习笔记8:标志库之正则表达式

Python拥有强大的标准库.从现在起,开始学习标准库中提供的一些常用功能. 首先看正则表达式(regular expression),它的主要功能是从字符串(string)中通过特定的模式(pattern),搜索想要找到的内容. 例如:要从一个字符串中找出所有的数字,我们可以这样做: import re str = "int2str" m = re.search("[0-9]",str) print(m.group(0)) 输出:2 re.search()接收两个