requests请求,就是用python的requests模块模拟浏览器请求,返回html源码html
模拟浏览器请求有两种,一种是不须要用户登陆或者验证的请求,一种是须要用户登陆或者验证的请求python
1、不须要用户登陆或者验证的请求web
这种比较简单,直接利用requests模块发一个请求便可拿到html源码浏览器
#!/usr/bin/env python # -*- coding:utf8 -*- import requests #导入模拟浏览器请求模块 http =requests.get(url="http://www.iqiyi.com/") #发送http请求 http.encoding = "utf-8" #http请求编码 neir = http.text #获取http字符串代码 print(neir)
获得html源码服务器
<!DOCTYPE html> <html> <head> <title>抽屉新热榜-聚合每日热门、搞笑、有趣资讯</title> <meta charset="utf-8" /> <meta name="keywords" content="抽屉新热榜,资讯,段子,图片,公众场合不宜,科技,新闻,节操,搞笑" /> <meta name="description" content=" 抽屉新热榜,汇聚每日搞笑段子、热门图片、有趣新闻。它将微博、门户、社区、bbs、社交网站等海量内容聚合在一块儿,经过用户推荐生成最热榜单。看抽屉新热榜,每日热门、有趣资讯一览无余。 " /> <meta name="robots" content="index,follow" /> <meta name="GOOGLEBOT" content="index,follow" /> <meta name="Author" content="搞笑" /> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8"> <link type="image/x-icon" href="/images/chouti.ico" rel="icon"/> <link type="image/x-icon" href="/images/chouti.ico" rel="Shortcut Icon"/> <link type="image/x-icon" href="/images/chouti.ico" rel="bookmark"/> <link type="application/opensearchdescription+xml" href="opensearch.xml" title="抽屉新热榜" rel="search" />
2、须要用户登陆或者验证的请求cookie
获取这种页面时,咱们首先要了解整个登陆过程,通常登陆过程是,当用户第一次访问时,会自动在浏览器生成cookie文件,当用户输入登陆信息后会携带着生成的cookie文件,若是登陆信息正确会给这个cookieapp
受权,受权后之后访问须要登陆的页面时携带受权后cookie便可post
一、首先访问一下首页,而后查看是否有自动生成cookie网站
#!/usr/bin/env python # -*- coding:utf8 -*- import requests #导入模拟浏览器请求模块 ### 一、在没登陆以前访问一下首页,获取cookie i1 = requests.get( url="http://dig.chouti.com/", headers={'Referer': 'http://dig.chouti.com/'} ) i1.encoding = "utf-8" #http请求编码 i1_cookie = i1.cookies.get_dict() print(i1_cookie) #返回获取到的cookie #返回:{'JSESSIONID': 'aaaTztKP-KaGLbX-T6R0v', 'gpsd': 'c227f059746c839a28ab136060fe6ebe', 'route': 'f8b4f4a95eeeb2efcff5fd5e417b8319'}
能够看到生成了cookie,说明若是登录信息正确,后台会给这里的cookie受权,之后访问须要登陆的页面携带受权后的cookie便可ui
二、让程序自动去登陆受权cookie
首先咱们用浏览器访问登陆页面,随便乱输入一下登陆密码和帐号,获取登陆页面url,和登陆所须要的字段
携带cookie登陆受权
#!/usr/bin/env python # -*- coding:utf8 -*- import requests #导入模拟浏览器请求模块 ### 一、在没登陆以前访问一下首页,获取cookie i1 = requests.get( url="http://dig.chouti.com/", headers={'Referer':'http://dig.chouti.com/'} ) i1.encoding = "utf-8" #http请求编码 i1_cookie = i1.cookies.get_dict() print(i1_cookie) #返回获取到的cookie #返回:{'JSESSIONID': 'aaaTztKP-KaGLbX-T6R0v', 'gpsd': 'c227f059746c839a28ab136060fe6ebe', 'route': 'f8b4f4a95eeeb2efcff5fd5e417b8319'} ### 二、用户登录,携带上一次的cookie,后台对cookie中的随机字符进行受权 i2 = requests.post( url="http://dig.chouti.com/login", #登陆url data={ #登陆字段 'phone': "8615284816568", 'password': "279819", 'oneMonth': "" }, headers={'Referer':'http://dig.chouti.com/'}, cookies=i1_cookie #携带cookie ) i2.encoding = "utf-8" dluxxi = i2.text print(dluxxi) #查看登陆后服务器的响应 #返回:{"result":{"code":"9999", "message":"", "data":{"complateReg":"0","destJid":"cdu_50072007463"}}} 登陆成功
三、登陆成功后,说明后台已经给cookie受权,这样咱们访问须要登陆的页面时,携带这个cookie便可,好比获取我的中心
#!/usr/bin/env python # -*- coding:utf8 -*- import requests #导入模拟浏览器请求模块 ### 一、在没登陆以前访问一下首页,获取cookie i1 = requests.get( url="http://dig.chouti.com/", headers={'Referer':'http://dig.chouti.com/'} ) i1.encoding = "utf-8" #http请求编码 i1_cookie = i1.cookies.get_dict() print(i1_cookie) #返回获取到的cookie #返回:{'JSESSIONID': 'aaaTztKP-KaGLbX-T6R0v', 'gpsd': 'c227f059746c839a28ab136060fe6ebe', 'route': 'f8b4f4a95eeeb2efcff5fd5e417b8319'} ### 二、用户登录,携带上一次的cookie,后台对cookie中的随机字符进行受权 i2 = requests.post( url="http://dig.chouti.com/login", #登陆url data={ #登陆字段 'phone': "8615284816568", 'password': "279819", 'oneMonth': "" }, headers={'Referer':'http://dig.chouti.com/'}, cookies=i1_cookie #携带cookie ) i2.encoding = "utf-8" dluxxi = i2.text print(dluxxi) #查看登陆后服务器的响应 #返回:{"result":{"code":"9999", "message":"", "data":{"complateReg":"0","destJid":"cdu_50072007463"}}} 登陆成功 ### 三、访问须要登陆才能查看的页面,携带着受权后的cookie访问 shouquan_cookie = i1_cookie i3 = requests.get( url="http://dig.chouti.com/user/link/saved/1", headers={'Referer':'http://dig.chouti.com/'}, cookies=shouquan_cookie #携带着受权后的cookie访问 ) i3.encoding = "utf-8" print(i3.text) #查看须要登陆才能查看的页面
获取须要登陆页面的html源码成功
所有代码
get()方法,发送get请求
encoding属性,设置请求编码
cookies.get_dict()获取cookies
post()发送post请求
text获取服务器响应信息
#!/usr/bin/env python # -*- coding:utf8 -*- import requests #导入模拟浏览器请求模块 ### 一、在没登陆以前访问一下首页,获取cookie i1 = requests.get( url="http://dig.chouti.com/", headers={'Referer':'http://dig.chouti.com/'} ) i1.encoding = "utf-8" #http请求编码 i1_cookie = i1.cookies.get_dict() print(i1_cookie) #返回获取到的cookie #返回:{'JSESSIONID': 'aaaTztKP-KaGLbX-T6R0v', 'gpsd': 'c227f059746c839a28ab136060fe6ebe', 'route': 'f8b4f4a95eeeb2efcff5fd5e417b8319'} ### 二、用户登录,携带上一次的cookie,后台对cookie中的随机字符进行受权 i2 = requests.post( url="http://dig.chouti.com/login", #登陆url data={ #登陆字段 'phone': "8615284816568", 'password': "279819", 'oneMonth': "" }, headers={'Referer':'http://dig.chouti.com/'}, cookies=i1_cookie #携带cookie ) i2.encoding = "utf-8" dluxxi = i2.text print(dluxxi) #查看登陆后服务器的响应 #返回:{"result":{"code":"9999", "message":"", "data":{"complateReg":"0","destJid":"cdu_50072007463"}}} 登陆成功 ### 三、访问须要登陆才能查看的页面,携带着受权后的cookie访问 shouquan_cookie = i1_cookie i3 = requests.get( url="http://dig.chouti.com/user/link/saved/1", headers={'Referer':'http://dig.chouti.com/'}, cookies=shouquan_cookie #携带着受权后的cookie访问 ) i3.encoding = "utf-8" print(i3.text) #查看须要登陆才能查看的页面
注意:若是登陆须要验证码,那就须要作图像处理,根据验证码图片,识别出验证码,将验证码写入登陆字段