爬虫之Requests库

素颜马尾好姑娘i 2023-02-20 04:58 179阅读 0赞

爬虫之Requests

  • requests库
    • 安装
    • 发送get请求
    • 发送post请求
    • 通过代理访问网站
  • 处理cookie

requests库

虽然python的标准库中urllib模块已经包含我们平常使用的大多数功能,但是它的API使用起来让人感觉不太好,而Requests使用起来更加方便

安装

利用pip 可以非常方便安装:

  1. pip install requests

发送get请求

  1. 最简单的发送get请求的方式就是通过requests.get调用:

    response=reqquests.get(“http://www.baidu.com/“)

  2. 添加headers和查询参数
    如果想要headers,可以传入headers参数来增加请求头中headers信息。如果要将参数传入url,可以利用params参数。具体实例如下:

    import requests

    kw={‘kw’:’中国’}
    headers={“Use-Agent”:}

    params 接收一个字典或者字符串查询参数,字典类型自动转化为url编码

    requests=requests.get(“http://www.baidu.com/s",params=kw,headers=headers)

    查看响应内容,requests.text返回的是unicode格式数据

    print(requests.text)

    查看响应内容,requests.content返回的是字节数据

    print(requests.content)

    查看完整url地址

    print(requests.url)

    查看响应头部字符编码

    print(requests.encoding)

    查看响应码

    print(requests.statu_code)

  3. requests.text和requests.content比较

requests.text实例

  1. import requests
  2. response=requests.get("http://www.baidu.com/")
  3. print(type(response.text))
  4. print(response.text)

requests.content实例

  1. #encoding utf-8
  2. import requests
  3. response=requests.get("http://www.baidu.com/")
  4. print(type(response.content))
  5. print(response.content.decode('utf-8'))

4.带着参数zhongguo访问百度页面

  1. import requests
  2. #response=requests.get("http://www.baidu.com/")
  3. #print(type(response.content))
  4. #print(response.content.decode('utf-8'))
  5. kw={'wd':'zhongguo'}
  6. headers={"Use-Agent":'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'}
  7. #params 接收一个字典或者字符串查询参数,字典类型自动转化为url编码
  8. response=requests.get("http://www.baidu.com/s",params=kw,headers=headers)
  9. #查看响应内容,requests.text返回的是unicode格式数据
  10. with open('baidu.html','w',encoding='utf-8') as fp:
  11. fp.write(response.content.decode('utf-8'))
  12. print(response.url)

发送post请求

发送post请求非常简单,直接调用’request.post’方法就可以
如果返回的是josn数据。那么可以调用’request.josn()’将josn字符串转换为字典或者列表。

  1. response=reqquests.post("http://www.baidu.com/,data-data")

接下来通过访问拉勾网来演示post请求

  1. #发送post请求
  2. import requests
  3. import time
  4. data={
  5. 'first':"ture",
  6. 'pn':'1',
  7. 'kd':'python'
  8. }
  9. url_start = "https://www.lagou.com/jobs/list_运维?city=%E6%88%90%E9%83%BD&cl=false&fromSearch=true&labelWords=&suginput="
  10. url_parse = "https://www.lagou.com/jobs/positionAjax.json?city=成都&needAddtionalResult=false"
  11. headers = {
  12. 'Accept': 'application/json, text/javascript, */*; q=0.01',
  13. 'Referer': 'https://www.lagou.com/jobs/list_%E8%BF%90%E7%BB%B4?city=%E6%88%90%E9%83%BD&cl=false&fromSearch=true&labelWords=&suginput=',
  14. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
  15. }
  16. s=requests.Session()# 创建一个session对象
  17. s.get(url=url_start,headers=headers,timeout=3)# session对象发出get请求,请求首页获取cookies
  18. cookie=s.cookies# 为此次获取的cookies
  19. response=requests.post("https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false",data=data,headers=headers,cookies=cookie,timeout=3)
  20. time.sleep(7)
  21. #print(cookie)
  22. #print(type(response.json()))
  23. print(response.text)

通过代理访问网站

使用代理很简单,只要在请求的方法中传递proxies参数就可以

实例如下:通过代理访问网站http://httpbin/ip就可以看到ip跟本机不一样

  1. #通过代理访问
  2. import requests
  3. #url="http://httpbin.org/ip"
  4. proxy={
  5. 'http':'171.35.147.108:9999'
  6. }
  7. respones=requests.get("http://httpbin.org/ip",proxies=proxy)
  8. print(respones.text)

处理cookie

如果想要在多次请求中共享cookie,那么就应该使用session。代码如下:

  1. #处理cookie
  2. #如果想要在多次请求中共享cookie,那么就应该使用session。代码如下:
  3. url="http://www.renren.com/PLogin.do"
  4. data={'email':'','password':''}
  5. headers={
  6. 'Use-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'
  7. }
  8. session=requests.session()
  9. session.post(url,data=data,headers=headers)
  10. respones=session.get('http://www.renren.com/880151247/profile')
  11. with open('renren.html','w',encoding='utf-8') as fp:
  12. fp.write(respones.text)

发表评论

表情:
评论列表 (有 0 条评论,179人围观)

还没有评论,来说两句吧...

相关阅读