爬虫学习笔记(1) 基本库之requsts库的使用

一. requests库的使用
(1)get方法
在requests中使用get方法请求网页,此时的请求方式是以get方式请求(常用的网页请求方式有两种:get方法和post方法),得到一个Response对象。使用status_code,text,cookies等方法可以得到返回的状态码,text类型的数据,cookies消息(作用的保存用户的登陆信息)。

import requests
response = requests.get('https://www.baidu.com')
print(response.status_code)
print(response.text)
print(response.cookies)

get请求还可以添加一些额外的参数,保证爬虫可以正常的运行,比如加headers就可以伪装成浏览器进行访问,设置timeout可以设置超时等待,添加cookies来维持登陆状态。

headers = {
    'Cookie': 'uuid_tt_dd=10_20994997240-1539219979697-474561;'
               +'Hm_ct_6bcd52f51e9b3dce32bec4a3997715ac=1788*1*PC_VC;smidV2=20181011182544ca524dd834a26990665a402683976d91009e5e501cce0e510; UN=qq_30003623;'
               +'dc_session_id=10_1539482863058.434263;UserName=qq_30003623;'
               +'UserInfo=mRrMNbuiaKJx7FJh5AZUj96rt%2Bu9782LZvCCD6ZO9tBY866JKY7rn57P5V7dE6oK1sjmkj83xLQkFn5O1ByAQw%3D%3D;'
               +'UserNick=qq_30003623; AU=34D;'
               +'BT=1539482908275;'
               +'UserToken=mRrMNbuiaKJx7FJh5AZUj96rt%2Bu9782LZvCCD6ZO9tBY866JKY7rn57P5V7dE6oKKs1QSEGWCM85gxZ2pp%2BWexs%2BJiuxFDXwz4%2Bs3siVw278R5qQqixHBYiie2JpKI6I;'
               +'Hm_lvt_6bcd52f51e9b3dce32bec4a3997715ac=1539483023,1539483188,1539483238,1539484519;'
               +'Hm_lpvt_6bcd52f51e9b3dce32bec4a3997715ac=1539485294;'
               +'dc_tos=pgkiw1',
    'Host': 'pv.csdn.net',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
      }
response = requests.get('https://www.baidu.com',headers = headers,allow_redirects=False)

可能会出现网页重定向的问题,所以加参数allow_redirects=False,阻止重定向。

上述例子返回的都是HTML文档,如果想抓取图片,音频,视频等信息就要拿到它们的二进制编码,调用content方法。

import requests
r = requests.get('https://github.com/favicon.ico')
print(r.content)

(2)POST请求
同样是请求网页的方法,post请求与get属于HTTP协议中发送请求的两种方法,底层都是TCP链接。但是get请求产生一个数据包,post请求产生两个数据包。具体的不同请参见https://www.cnblogs.com/logsharing/p/8448446.html,同样在requests中使用post请求也非常简单。

import requests
data = {'name':'Bob','age':'22'}
r = requests.post('http://httpbin.org/post',data = data)

运行结果如下:

{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "age": "22", 
    "name": "Bob"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "15", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.11.1"
  }, 
  "json": null, 
  "origin": "111.32.91.13", 
  "url": "http://httpbin.org/post"
}

应用post方法还可以模拟提交一些数据,比如向网站上传一些数据。

import requests
files = {'file':open('favicon.ico','rb')}
r = requests.post('http://httpbin.org/post',files = files)
print(r.text)

(3)会话维持
用通俗一点的话来说,就是访问一个网站下的不同页面时,开启会话维持就相当于在已经登陆网站的状态下,打开一个新的选项卡。如果不开的话,就相当于点开两个不同的浏览器访问这个网站。

import requests
s = requests.Session()
s.get('http://httpbin.org/cookies/set/number/123456789')
r = s.get(http://httpbin.org/cookies)
print(r.text)

先设置网页的cookies,再获取当前网页的cookies,维持同一个会话的情况下就可以正常获取结果。如果不设置会话的话,新打开的网页自身并没有设置cookies,所以不能得到正确的结果。运行结果如下:

{
  "cookies": {
    "number": "123456789"
  }
}

猜你喜欢

转载自blog.csdn.net/qq_30003623/article/details/83046053
今日推荐