requests模块

  使用requests可以模拟浏览器的请求,requests模块的本质是封装了urllib3模块的功能,比起之前用到的urllib,requests模块的api更加便捷

  requests库发送请求将网页内容下载下来以后,并不会执行js代码,这需要我们自己分析目标站点然后发起新的request请求,但是selenium模块就可以执行js的操作。

安装:

pip3 install requests  

请求方式:主要用到的就get和post两种

#各种请求方式:常用的就是requests.get()和requests.post()
import requests

r = requests.get('https://api.github.com/events')
r = requests.post('http://httpbin.org/post', data = {'key':'value'})
r = requests.put('http://httpbin.org/put', data = {'key':'value'})
r = requests.delete('http://httpbin.org/delete')
r = requests.head('http://httpbin.org/get')
r = requests.options('http://httpbin.org/get')
#get请求
http默认的请求方法就是get
   * 没有请求体
   * 数据必须在1k之内!
   * get请求数据会暴露在浏览器的地址栏中

get请求常用的操作:
    1. 在浏览器的地址栏中直接给出url,那么就一定是get请求
    2. 点击页面上的超链接也一定是get请求
    3. 提交表单时,表单默认使用get请求,但可以设置为post


#post请求
(1). 数据不会出现在地址栏中
(2). 数据的大小没有上限
(3). 有请求体
(4). 请求体中如果存在中文,会使用url编码!


#!!!requests.post()用法与requests.get()完全一致,特殊的是requests.post()有一个data参数,用来存放请求体数据

基于get的请求方式

一、基本请求的代码

import requests
response=requests.get('http://http://www.cnblogs.com/')
print(response.text)

二、get请求的参数

  get请求的参数放在url的问号后面,以键值对形式传参

方式一:自行拼接参数(原理就是这样,不过我们一般都用方式二的形式)

#在请求头内将自己伪装成浏览器,否则百度不会正常返回页面内容
import requests

response=requests.get('https://www.baidu.com/s?wd=python&pn=1',
           #请求头信息
           headers={
            'user-agent':'mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/62.0.3202.75 safari/537.36',
           })
print(response.text)


#如果查询关键词是中文或者有其他特殊符号,则不得不进行url编码后再拼接
from urllib.parse import urlencode
wd='苍老师'
encode_res=urlencode({'k':wd},encoding='utf-8')
keyword=encode_res.split('=')[1]#拿到编码后的字符串
print(keyword)
# 然后拼接成url
url='https://www.baidu.com/s?wd=%s&pn=1' %keyword

response=requests.get(url,
           headers={
            'user-agent':'mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/62.0.3202.75 safari/537.36',
           })
res1=response.text

方式二:利用params参数(原理就是底层封装了方式一)

#上述操作可以用requests模块的一个params参数搞定,本质还是调用urlencode
from urllib.parse import urlencode
wd='egon老师'
pn=1

response=requests.get('https://www.baidu.com/s',
           #参数进行传参,帮我们省去了urlencode这一步
           params={
             'wd':wd,
             'pn':pn
           },
           headers={
            'user-agent':'mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/62.0.3202.75 safari/537.36',
           })
res2=response.text

#验证结果,打开a.html与b.html页面内容一样
with open('a.html','w',encoding='utf-8') as f:
  f.write(res1) 
with open('b.html', 'w', encoding='utf-8') as f:
  f.write(res2)

params参数的使用

三、请求头headers

  一般情况下,浏览器在发送get请求的时候都会有请求头,用于放置跟客户端有关的信息。有些网站必须要有某些参数,这时我们在用爬虫伪造浏览器发送get请求时就要在headers参数下设置好请求头中会携带的参数

常见请求头:

#通常我们在发送请求时都需要带上请求头,请求头是将自身伪装成浏览器的关键,常见的有用的请求头如下
host
referer  #大型网站通常都会根据该参数判断请求的来源
user-agent #客户端
cookie   #cookie信息包含在请求头里,requests模块有单独的参数来处理他,处理后headers={}内就可以不用放置
#添加headers(浏览器会识别请求头,不加可能会被拒绝访问,比如访问https://www.zhihu.com/explore)
import requests
response=requests.get('https://www.zhihu.com/explore')
response.status_code #500


#自己定制headers
headers={
  'user-agent':'mozilla/5.0 (linux; android 6.0; nexus 5 build/mra58n) applewebkit/537.36 (khtml, like gecko) chrome/46.0.2490.76 mobile safari/537.36',

}
respone=requests.get('https://www.zhihu.com/explore',
           headers=headers)
print(respone.status_code) #200

设置请求头的方式

四、cookie信息

  有些要登录才能实行的功能,我们就必须在发起请求的信息中带上cookie,该cookie需要先由浏览器真实访问后得到,不可伪造。但我们可以在浏览器真实访问后将cookie信息复制出来,在我们爬虫发起get请求时传入该参数即可

#登录github,然后从浏览器中获取cookies,以后就可以直接拿着cookie登录了,无需输入用户名密码
#用户名:egonlin 邮箱378533872@qq.com 密码lhf@123
import requests

cookies={    'user_session':'wgmhfjkgdcmrivvca14_wrt_3xauyjnsbnpbyzel6l0bhcfc',
}

response=requests.get(        'https://github.com/settings/emails',
       cookies=cookies) #github对请求头没有什么限制,我们无需定制user-agent,对于其他网站可能还需要定制


print('378533872@qq.com' in response.text) #true

  然而,每次都要先打开浏览器访问链接再讲cookie复制出来是一个非常麻烦的事,真正的程序员怎么可以被这个给限制住。所以,我们就要用到requests模块下的一个函数

import requests
 
session = requests.session()

  之后,我们每次发起get请求的时候都用session代替requests即可,再也不必为传cookie而烦恼啦。(从此忘记cookie也行哈哈哈)

示例代码:

session.get(          'https://passport.lagou.com/grantserviceticket/grant.html',
         headers={
           'user-agent': 'mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/63.0.3239.132 safari/537.36',
           'referer': 'https://passport.lagou.com/login/login.html',
         }
         )

基于post的请求方式

  请求的代码与get相同,不过因为post请求的参数放在请求体里,所以在传参的时候会多一个data参数,用于放置请求的参数信息。

'''
一 目标站点分析
  浏览器输入https://github.com/login
  然后输入错误的账号密码,抓包
  发现登录行为是post提交到:https://github.com/session
  而且请求头包含cookie
  而且请求体包含:
    commit:sign in
    utf8:
    authenticity_token:lbi8ijcwgslzs8qjpnof5e7zkcosomn6jmdtsl1r/m06nlyibw7vcrpwrfapzhmep3tmf/tsjvoxwrvdzavwxq==
    login:egonlin
    password:123


二 流程分析
  先get:https://github.com/login拿到初始cookie与authenticity_token
  返回post:https://github.com/session, 带上初始cookie,带上请求体(authenticity_token,用户名,密码等)
  最后拿到登录cookie

  ps:如果密码时密文形式,则可以先输错账号,输对密码,然后到浏览器中拿到加密后的密码,github的密码是明文
'''

import requests
import re

#第一次请求
r1=requests.get('https://github.com/login')
r1_cookie=r1.cookies.get_dict() #拿到初始cookie(未被授权)
authenticity_token=re.findall(r'name="authenticity_token".*?value="(.*?)"',r1.text)[0] #从页面中拿到csrf token

#第二次请求:带着初始cookie和token发送post请求给登录页面,带上账号密码
data={
  'commit':'sign in',
  'utf8':'',
  'authenticity_token':authenticity_token,
  'login':'317828332@qq.com',
  'password':'alex3714'
}
r2=requests.post('https://github.com/session',
       data=data,
       cookies=r1_cookie
       )


login_cookie=r2.cookies.get_dict()


#第三次请求:以后的登录,拿着login_cookie就可以,比如访问一些个人配置
r3=requests.get('https://github.com/settings/emails',
        cookies=login_cookie)

print('317828332@qq.com' in r3.text) #true

自动登录github(自己处理cookie信息)

手动处理cookie后实现自动登录github
import requests
import re

session=requests.session()
#第一次请求
r1=session.get('https://github.com/login')
authenticity_token=re.findall(r'name="authenticity_token".*?value="(.*?)"',r1.text)[0] #从页面中拿到csrf token

#第二次请求
data={
  'commit':'sign in',
  'utf8':'',
  'authenticity_token':authenticity_token,
  'login':'317828332@qq.com',
  'password':'alex3714'
}
r2=session.post('https://github.com/session',
       data=data,
       )

#第三次请求
r3=session.get('https://github.com/settings/emails')

print('317828332@qq.com' in r3.text) #true
使用requests.session()自动帮我们处理cookie信息后自动登录github

补充说明

requests.post(url='xxxxxxxx',
       data={'xxx':'yyy'}) #没有指定请求头,#默认的请求头:application/x-www-form-urlencoed

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


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

响应response

  response是响应信息,具体属性如下

import requests
respone=requests.get('http://www.jianshu.com')
# respone属性
print(respone.text)  #包含html内容的字符串
print(respone.content)#bytes类型的字符串

print(respone.status_code)#状态码200
print(respone.headers)  #请求头信息
print(respone.cookies)  #cookie对象
print(respone.cookies.get_dict())#封装了cookie参数的字典{'locale': 'zh-cn'}

print(respone.cookies.items())#封装了cookie参数的列表[('locale', 'zh-cn')]

print(respone.url)#url地址https://www.jianshu.com/
print(respone.history)

print(respone.encoding)#编码utf-8

#关闭:response.close()
from contextlib import closing
with closing(requests.get('xxx',stream=true)) as response:
  for line in response.iter_content():
  pass#将内容一行一行写到文件中

有时网页不是utf-8的编码,所以我们要解决编码的问题

#编码问题
import requests
response=requests.get('http://www.autohome.com/news')
# response.encoding='gbk' #汽车之家网站返回的页面内容为gb2312编码的,而requests的默认编码为iso-8859-1,如果不设置成gbk则中文乱码
print(response.text)

获取二进制数据和二进制流

  图片和视频我们获取下来时是二进制数据,这时我们要用wb的形式写到本地。但有时视频很大,我们一下子加载到内存再去写入本地是不合理的,所以我们会用到二进制流

写二进制数据:

import requests

response=requests.get('https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1509868306530&di=712e4ef3ab258b36e9f4b48e85a81c9d&imgtype=0&src=http%3a%2f%2fc.hiphotos.baidu.com%2fimage%2fpic%2fitem%2f11385343fbf2b211e1fb58a1c08065380dd78e0c.jpg')

with open('a.jpg','wb') as f:
  f.write(response.content)

二进制流:

#stream参数:一点一点的取,比如下载视频时,如果视频100g,用response.content然后一下子写到文件中是不合理的

import requests

response=requests.get('https://gss3.baidu.com/6lz0ej3k1qd3ote6lo7d0j9wehsv/tieba-smallvideo-transcode/1767502_56ec685f9c7ec542eeaf6eac93a65dc7_6fe25cd1347c_3.mp4',
           stream=true)

with open('b.mp4','wb') as f:
  for line in response.iter_content():
    f.write(line)

解析json数据:

#解析json
import requests
response=requests.get('http://httpbin.org/get')

import json
res1=json.loads(response.text) #太麻烦

res2=response.json() #直接获取json数据

print(res1 == res2) #true

redirection and history

by default requests will perform location redirection for all verbs except head.

we can use the history property of the response object to track redirection.

the response.history list contains the response objects that were created in order to complete the request. the list is sorted from the oldest to the most recent response.

for example, github redirects all http requests to https:

>>> r = requests.get('http://github.com')

>>> r.url
'https://github.com/'

>>> r.status_code

>>> r.history
[<response [301]>]
if you're using get, options, post, put, patch or delete, you can disable redirection handling with the allow_redirects parameter:

>>> r = requests.get('http://github.com', allow_redirects=false)

>>> r.status_code

>>> r.history
[]
if you're using head, you can enable redirection as well:

>>> r = requests.head('http://github.com', allow_redirects=true)

>>> r.url
'https://github.com/'

>>> r.history
[<response [301]>]

先看官网的解释

官网解释(强行装作看得懂的样子)
import requests
import re

#第一次请求
r1=requests.get('https://github.com/login')
r1_cookie=r1.cookies.get_dict() #拿到初始cookie(未被授权)
authenticity_token=re.findall(r'name="authenticity_token".*?value="(.*?)"',r1.text)[0] #从页面中拿到csrf token

#第二次请求:带着初始cookie和token发送post请求给登录页面,带上账号密码
data={
  'commit':'sign in',
  'utf8':'',
  'authenticity_token':authenticity_token,
  'login':'317828332@qq.com',
  'password':'alex3714'
}






#测试一:没有指定allow_redirects=false,则响应头中出现location就跳转到新页面,r2代表新页面的response
r2=requests.post('https://github.com/session',
       data=data,
       cookies=r1_cookie
       )

print(r2.status_code) #200
print(r2.url) #看到的是跳转后的页面
print(r2.history) #看到的是跳转前的response
print(r2.history[0].text) #看到的是跳转前的response.text


#测试二:指定allow_redirects=false,则响应头中即便出现location也不会跳转到新页面,r2代表的仍然是老页面的response
r2=requests.post('https://github.com/session',
       data=data,
       cookies=r1_cookie,
       allow_redirects=false
       )


print(r2.status_code) #302
print(r2.url) #看到的是跳转前的页面https://github.com/session
print(r2.history) #[]

利用github登录后跳转到主页面的例子来验证它

进阶知识点

  其实学会了以上的知识,就已经可以做到用requests去爬网页了。下面的知识是高级用法,但其实并不难。

一、ssl证书验证

  很多大网站用的都是https,而https是要求访问者携带证书的,但是其实有些不用证书也可以访问,大多数情况都是可以携带也可以不携带证书比如知乎和百度等

但有些是有硬性要求的,则必须带,比如对于定向的用户,拿到证书后才有权限访问。例如12306

  这个时候我们在用爬虫发起请求的时候就需要做一些手脚啦

#证书验证(大部分网站都是https)
import requests
respone=requests.get('https://www.12306.cn') #如果是ssl请求,首先检查证书是否合法,不合法则报错,不允许访问


#改进1:去掉报错,能访问但是会报警告,因为毕竟我们没有证书
import requests
respone=requests.get('https://www.12306.cn',verify=false) #verify参数改成false就不验证证书,报警告,能访问,返回200
print(respone.status_code)#200


#改进2:去掉报错,并且去掉警报信息,与上步没啥太大区别
import requests
from requests.packages import urllib3
urllib3.disable_warnings() #关闭警告
respone=requests.get('https://www.12306.cn',verify=false)
print(respone.status_code)

#改进3:加上证书
#很多网站都是https,但是不用证书也可以访问,大多数情况都是可以携带也可以不携带证书
#知乎\百度等都是可带可不带
#有硬性要求的,则必须带,比如对于定向的用户,拿到证书后才有权限访问某个类似12306的特定网站
import requests
respone=requests.get('https://www.12306.cn',
           cert=('/path/server.crt',
              '/path/key'))#证书存放在本地
print(respone.status_code)

二、代理

  有些时候,我们用爬虫访问某网站请求的太频繁时人家网站会检测到你的不法行为然后将你的ip封掉,这样你就不能继续做不可告人的事啦。怎么办呢?我们可以用到代理。

代理的原理是:我们先发送请求到代理的ip上,然后由代理帮忙发送。代理ip可以在网上搜,一搜一大把,但代理也是有时效性的,尽量用最新鲜的,因为老旧的可能已经被人用烂然后早就被封了。。

使用http代理的栗子:

#官网链接: http://docs.python-requests.org/en/master/user/advanced/#proxies

#代理设置:先发送请求给代理,然后由代理帮忙发送
import requests
#代理的参数
proxies={
  'http':'http://egon:123@localhost:9743',#带用户名密码的代理,@符号前是用户名与密码,@符号后是ip和端口
  'http':'http://localhost:9743',#不带用户名密码的代理
  'https':'https://localhost:9743',
}
respone=requests.get('https://www.12306.cn',
           proxies=proxies)

print(respone.status_code)

扩展:socks代理

  socks 代理与应用层代理、 http 层代理不同,socks代理只是简单地传递数据包,而不必关心是何种应用协议(比如ftp、http和nntp请求)。所以,socks代理比其他应用层代理要快得多。它通常绑定在代理服务器的1080端口上。如果在企业网或校园网上,需要透过防火墙或通过代理服务器访问internet就可能需要使用socks。
  一般情况下,对于拨号上网用户都不需要使用它。我们常用的代理服务器仍然是专门的http代理,它和socks是不同的。因此,能浏览网页不等于您一定可以通过socks访问internet。 常用的防火墙,或代理软件都支持socks,但需要其管理员打开这一功能。为了使用socks,需要了解一下内容:
  ① socks服务器的ip地址
  ② socks服务所在的端口
  ③ 这个socks服务是否需要用户认证?如果需要,就要向网络管理员申请一个用户和口令
知道了上述信息,您就可以把这些信息填入“网络配置”中,或者在第一次登记时填入,您就可以使用socks代理了。
  不过我们在爬虫阶段不必了解那么多,我们只要知道用http代理的时候必须区分开是http请求还是https请求,因为代码是不同的。而利用socks代理的时候就可以五十这些,统一使用socks。

示例代码:

#支持socks代理,安装:pip install requests[socks]
import requests

#用socks代替了http和https,无需特意做区分
proxies = {
  'http': 'socks5://user:pass@host:port',
  'https': 'socks5://user:pass@host:port'
}
respone=requests.get('https://www.12306.cn',
           proxies=proxies)

print(respone.status_code)

三、超时时间

  有时候网络不好或者我们有相应的需求的情况下,我们要为我们的网页加载设置一个超时时间。在过了超时时间后网页还没加载完就关闭此次进程

#两种超时设置:float or tuple
#timeout=0.1 #代表接收数据的超时时间
#timeout=(0.1,0.2)#0.1代表链接的超时时间 0.2代表接收数据的超时时间

import requests
respone=requests.get('https://www.baidu.com',
           timeout=0.01)

四、认证设置(极不常用,了解即可)

  有些网站的登录框是像alert一样弹出来的,这种类型的就无法从html中获取到的,但其本质原理还是拼接成请求头发送的。

注:一般网站都不会使用这种形式的登录界面

r.headers['authorization'] = _basic_auth_str(self.username, self.password)
# 一般的网站都不用默认的加密方式,都是自己写
# 那么我们就需要按照网站的加密方式,自己写一个类似于_basic_auth_str的方法得到加密字符串后添加到请求头
#     r.headers['authorization'] =func('.....')

#默认的加密方式(不过通常网站都不会用默认的加密设置)
import requests
from requests.auth import httpbasicauth
r=requests.get('xxx',auth=httpbasicauth('user','password'))
print(r.status_code)

#httpbasicauth可以简写为auth
import requests
r=requests.get('xxx',auth=('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 requestexception:
  print('error')

六、上传文件

  上传文件的操作很简单,但是其实我们在爬虫的时候基本不会用到上传,我们都是爬取的。

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

 formdata格式上传图片

import requests
from urllib3 import encode_multipart_formdata

path = 'test1.png'

# 上传图片
url = 'xxxxxx'
data = {'upload':(path, open(path, 'rb').read())}
header = {}
encode_data = encode_multipart_formdata(data)
file_data = encode_data[0]
header['content-type'] = encode_data[1]
r = requests.post(url, headers=header, data=file_data)

简单应用

爬取拉勾网职位信息并自动投递简历:

import requests
import re

session = requests.session()

# 第一步:访问登陆页,拿到x_anti_forge_token,x_anti_forge_code
# 1、请求url:https://passport.lagou.com/login/login.html
# 2、请求方法:get
# 3、请求头:
#  user-agent
r1 = session.get('https://passport.lagou.com/login/login.html',
         headers={
           'user-agent': 'mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/63.0.3239.132 safari/537.36',
         },
         )

x_anti_forge_token = re.findall("x_anti_forge_token = '(.*?)'", r1.text, re.s)[0]
x_anti_forge_code = re.findall("x_anti_forge_code = '(.*?)'", r1.text, re.s)[0]
# print(x_anti_forge_token,x_anti_forge_code)

# 第二步:登陆
# 1、请求url:https://passport.lagou.com/login/login.json
# 2、请求方法:post
# 3、请求头:
#  cookie
#  user-agent
#  referer:https://passport.lagou.com/login/login.html
#  x-anit-forge-code:53165984
#  x-anit-forge-token:3b6a2f62-80f0-428b-8efb-ef72fc100d78
#  x-requested-with:xmlhttprequest
# 4、请求体:
# isvalidate:true
# username:18611453110
# password:70621c64832c4d4d66a47be6150b4a8e
# request_form_verifycode:''
# submit:''
r2 = session.post('https://passport.lagou.com/login/login.json',
         headers={
           'user-agent': 'mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/63.0.3239.132 safari/537.36',
           'referer': 'https://passport.lagou.com/login/login.html',
           'x-anit-forge-code': x_anti_forge_code,
           'x-anit-forge-token': x_anti_forge_token,
           'x-requested-with': 'xmlhttprequest'
         },
         data={
           "isvalidate": true,
           'username': '18611453110',
           'password': '70621c64832c4d4d66a47be6150b4a8e',
           'request_form_verifycode': '',
           'submit': ''
         }
         )
# 第三步:授权
# 1、请求url:https://passport.lagou.com/grantserviceticket/grant.html
# 2、请求方法:get
# 3、请求头:
#  user-agent
#  referer:https://passport.lagou.com/login/login.html

r3 = session.get('https://passport.lagou.com/grantserviceticket/grant.html',
         headers={
           'user-agent': 'mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/63.0.3239.132 safari/537.36',
           'referer': 'https://passport.lagou.com/login/login.html',
         }
         )

# 第四步:验证
r4 = session.get('https://www.lagou.com/resume/myresume.html',
         headers={
           'user-agent': 'mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/63.0.3239.132 safari/537.36',
         }
         )

# print('18611453110' in r4.text)


# 第五步:筛选职位信息
# 请求url:https://www.lagou.com/jobs/list_java%e9%ab%98%e7%ba%a7%e5%bc%80%e5%8f%91
# 请求方法:get
# 请求头:
# user-agent
# 请求参数:
# gj:3年及以下
# px:default
# yx:25k-50k
# city:北京
from urllib.parse import urlencode

res = urlencode({'k': 'java高级开发'}, encoding='utf-8').split('=')[-1]
url = 'https://www.lagou.com/jobs/list_' + res
#
# r5 = session.get(url,
#         headers={
#           'user-agent': 'mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/63.0.3239.132 safari/537.36',
#         },
#         params={
#           'gj': '3年及以下',
#           'px': 'default',
#           'yx': '25k-50k',
#           'city': '北京'
#         }
#         )
#
# print(r5.text)

#请求url:https://www.lagou.com/jobs/positionajax.json
#请求方法:post
#请求头
#  referer
#  user-agent
#请求体:
  # first:true
  # pn:1
  # kd:java高级开发
#请求参数
# params={
#   'gj': '3年及以下',
#   'px': 'default',
#   'yx': '25k-50k',
#   'city': '北京',
#   'needaddtionalresult':false,
#   'isschooljob':0
# }
r6=session.post('https://www.lagou.com/jobs/positionajax.json',
       headers={
          'referer':url,
           'user-agent': 'mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/63.0.3239.132 safari/537.36',

       },
       data={
         'first':true,
         'pn':2,
         'kd':'java高级开发'
       },
       params={
         'gj': '3年及以下',
         'px': 'default',
         'yx': '25k-50k',
         'city': '北京',
         'needaddtionalresult': false,
         'isschooljob': 0
       }
       )


from pprint import pprint
# print(r6.json())
comapines_list=r6.json()['content']['positionresult']['result']
for comapiny in comapines_list:
  positionid=comapiny['positionid']
  company_link='https://www.lagou.com/jobs/{pos_id}.html'.format(pos_id=positionid)
  companyshortname = comapiny['companyshortname']
  positionname = comapiny['positionname']
  salary = comapiny['salary']
  print('''
  详情连接:%s
  公司名:%s
  职位名:%s
  薪资:%s
  ''' %(company_link,companyshortname,positionname,salary))


  #第七步:访问详情页,拿到x_anti_forge_token,x_anti_forge_code
  # 请求url:详情页地址
  # 请求方式:get
  # 请求头:user-agent
  r7=session.get(company_link,
        headers={
          'user-agent': 'mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/63.0.3239.132 safari/537.36',
        }
        )
  x_anti_forge_token = re.findall("x_anti_forge_token = '(.*?)'", r7.text, re.s)[0]
  x_anti_forge_code = re.findall("x_anti_forge_code = '(.*?)'", r7.text, re.s)[0]
  # print(x_anti_forge_token,x_anti_forge_code)


  #第八步:投递简历
  #请求url:https://www.lagou.com/mycenterdelay/deliverresumebeforce.json
  #请求方式:post
  #请求头:
    #referer:详情页地址
    #user-agent
    #x-anit-forge-code:53165984
    #x-anit-forge-token:3b6a2f62-80f0-428b-8efb-ef72fc100d78
    #x-requested-with:xmlhttprequest
  #请求体:
  # positionid:职位id
  # type:1
  # force:true

  session.post('https://www.lagou.com/mycenterdelay/deliverresumebeforce.json',
         headers={
           'user-agent': 'mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/63.0.3239.132 safari/537.36',
           'referer': company_link,
           'x-anit-forge-code': x_anti_forge_code,
           'x-anit-forge-token': x_anti_forge_token,
           'x-requested-with': 'xmlhttprequest'
         },
         data={
  'positionid':positionid,
  'type':1,
  'force':true
         }
         )
  print('%s 投递成功' %(companyshortname))

爬取猎聘网职位信息(优化)

import re
import requests

session=requests.session()#可以帮助我们自动处理cookie信息,我们不用再手动穿cookie的值


# 第一步:访问登陆页,这里的登录页就是首页
# 1、请求url:https://www.liepin.com/
# 2、请求方法:get
# 3、请求头:
#  user-agent
r1_url='https://www.liepin.com/'
user_agent='mozilla/5.0 (windows nt 6.3; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/63.0.3239.108 safari/537.36'



# 第二步:登陆
# 1、请求url:https://passport.liepin.com/c/login.json?__mn__=user_login
# 2、请求方法:post
# 3、请求头:
#  cookie
#  user-agent
#  referer:https://passport.liepin.com/ajaxproxy.html
#  x-alt-referer:https://www.liepin.com/
#  x-requested-with:xmlhttprequest
# 4、请求体:
# user_pwd:5d353bc2f5474201c8a2f1891735999a
# version:
# user_login:15011316546
# chk_remember_pwd:on
r2_url='https://passport.liepin.com/c/login.json?__mn__=user_login'
r2_header_dict={
  'user-agent':user_agent,
  'referer':'https://passport.liepin.com/ajaxproxy.html',
  'x-alt-referer':'https://www.liepin.com/',
  'x-requested-with':'xmlhttprequest'
}
r2_form_data={
  'user_pwd':'5d353bc2f5474201c8a2f1891735999a',
  'user_login':'15011316546',
  'chk_remember_pwd':'on'
}




# 第三步:筛选职位信息
# 请求url:https://www.liepin.com/zhaopin/
# 请求方法:get
# 请求头:
# user-agent
# 请求参数:
# salary:15$20
# dqs:010
# key:前台
r3_url='https://www.liepin.com/zhaopin/'
r3_header_dict={
  'user-agent':user_agent
}
# r3_params={
#   'salary':'10$20',
#   'dqs':'010',
#   'key':'前台'
# }
r3_params={
  'salary':'10$20',
  'dqs':'070020',
  'key':'python开发'
}



#第四步 正则匹配 拿到页面中所有的职位数据,写入文件中
re_rule = 'class="icon icon-yellow-triangle".*?class="job-info".*?title="(.*?)".*?href="(.*?)" rel="external nofollow" .*?title="(.*?)".*?class="company-name".*?title="(.*?)".*?target="_blank">(.*?)</a>'
txt = '猎聘网招聘(python1).txt'

#封装,便于传参
dict={
  'r1_url':r1_url,
  'user_agent':user_agent,
  'r2_url':r2_url,
  'r2_header_dict':r2_header_dict,
  'r2_form_data':r2_form_data,
  'r3_url':r3_url,
  'r3_header_dict':r3_header_dict,
  'r3_params':r3_params,
  're_rule':re_rule,
  'txt':txt
}

def spider(**kwargs):
  #第一步
  r1=session.get(r1_url,
          headers={
            'referer':'https://www.liepin.com/',
            'user-agent':user_agent
          }
          )
  #第二步
  r2=session.post(r2_url,
          headers=r2_header_dict,
          data=r2_form_data
  )
  # 第三步3
  r3=session.get(
    r3_url,
    headers=r3_header_dict,
    params=r3_params
  )
  print('国内某知名投行' in r3.text)


  #第四步
  result=re.findall(re_rule,r3.text,re.s)
  for i in result:
    a="""
    招聘职位:%s
    招聘链接:%s
    薪资及要求:%s
    公司名称:%s
    公司类型:%s
     
    """%i
    with open(txt, 'a',encoding='utf-8') as f:
      f.write(a)




if __name__ == '__main__':
  spider(**dict)

以上就是python requests库的使用的详细内容,更多关于python requests库的资料请关注www.887551.com其它相关文章!