• python之爬虫


    一、爬虫之requests模块


     

    需求:给关键字到各在网站搜索存入数据库

    1.概述: requests模拟浏览器向后端发请求

                 requests模块如何安装:pip3 install requests

                 import requests

                 get请求:

                 response=requests.get('https://www.sogou.com/web?query=小虎')//发get请求,封装到response里了

                 print(response.text)

                 post请求:

                 form_data={   

                            'phone':18912020023

                            'password':'djfkjf'           

                    }

                   response=requests.post(

                              url='http://dig.chouti.com/login'

                              data=form_data

                   )

                  print(response.text)

                   requests.put()  requests.delete() requests.head()  requests.options()

    2.requests的基本参数

         (1)另一种调用方法:

                  requests.request(

                           method='get'

                            url='https://www.sogou.com/web'

                            params={'query':小虎,'q':'b'}//会自己拼接url:https://www.sogou.com/web?query=小虎&q=b

                            data={}//放请求体里的

                             cookie={'.CNBlogsCookie':'....'}

                  )

                  requests.request(

                           method='post'

                            url='https://www.sogou.com/web'

                            params={'query':小虎,'q':'b'}//会自己拼接url:https://www.sogou.com/web?query=小虎&q=b

                            data={'user':'dan'}//放请求体里的,可字典可字符串#"user=dan;pwd=sdfsdf":自己转成字符串,默认带请求头application/x-www-form-urlencoded

                            json=json.dumps({'user':'dan'})//默认带上请求头变成application/json

                              //有二种情况会出现浏览器能访问到数据,用这个请求就加获取不到数据,有二种情况如下:

                            headers={       请求头,是后台拿不到的数据

                                   'User-Agent':' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36 ',//终端是什么,把浏览器的终端信息拿过来放进去

                                   'Referer':'https://www.zhihu.com',//第二次发请求时要有的

                            }

                            cookies={'.CNBlogsCookie':'....'},//流程:用户会话保持,第一次登录成功后会发一个随机字符串

                           其它参数:

                            files,auth,proxies等

                           

                             )

                 def index(request):

                        request.method

                        request.GET.get('query')//后台拿的是params的字典,在url里拿数据

                        request.POST.get('user')//后台拿到的是data里的数据,在请求体里拿数据

                        request.body:拿到的是传输的字符串

                                       先判断请求头里是不是content-type:application/x-www-form-urlencoded,如果是把传输数据赋给body

                                       再把数据转成字典给POST,如果请求头不是这样比如是application/json就不转换直接给body,POST就是空

                  

                 

  • 相关阅读:
    vue子父组件传值
    springboot后端controller参数接收
    mybatis-plus 相关
    整理 node-sass 安装失败的原因及解决办法
    vue组件name的作用小结
    关于npm audit fix
    Vue项目
    你们都在用IntelliJ IDEA吗?或许你们需要看一下这篇博文
    Eslint配置
    spring boot 资料整合
  • 原文地址:https://www.cnblogs.com/Dana-xiong/p/14672880.html
Copyright © 2020-2023  润新知