python爬蟲開發 (一)-------requests模組

來源:互聯網
上載者:User

標籤:xxx   agent   font   python爬蟲   img   請求   使用者登入   偽造   nbsp   

requests:

    requests函數(可通過pycharm查看詳細函數):  -requests.get()  requests.post()  requests.put()  requests.delet()    .........    requests.request()

    參數:requests.request()

        -method:    提交方式

        -url:       提交地址

        -params     在url中傳遞的參數,get

  例:  

1 requests.request(2 3         method = "get",4         url = "www.ekankan.com"5         params = {"k1":"v1","k2":"v2"}6 )7 #http://www.www.ekankan.com?k1=v1&k2=v2

        -data:      在請求體裡傳遞的資料

 1 requests.request( 2   3          method = "get", 4          url = "www.ekankan.com", 5          params = {"k1":"v1","k2":"v2", 6                         data = {‘uers‘:‘alex‘,‘pwd‘:‘123‘}    7 } 8   9 10 ----------------------->#相當於將字典轉化為字串11 requests.request(12  13          method = "get",14          url = "www.ekankan.com",15          params = {"k1":"v1","k2":"v2",16                         data = ‘uers=alex&pwd=123‘17 }

 

        -json      在請求體裡傳遞的資料

 

1 requests.request(2     method = ‘get‘,3     url =    ‘www.ekankan.com‘,4     params = {‘k1‘:‘v1‘,‘k2‘:‘v2‘},5     json =      {‘uers‘:‘alex‘,‘pwd‘:‘123‘}6     7     #內部轉化:"{‘uers‘:‘alex‘,‘pwd‘:‘123‘}"8 9 )

 #思考    如何設定反爬機制: (源碼中的 referer  記錄你上一次登陸的網站)

 

         -headers        要求標頭

 1 requests.request( 2      method = ‘get‘, 3      url =    ‘www.ekankan.com‘, 4      params = {‘k1‘:‘v1‘,‘k2‘:‘v2‘}, 5      json =      {‘uers‘:‘alex‘,‘pwd‘:‘123‘} 6  7      headers = { 8       ‘Referer‘:‘www.ekankan.com/‘ 9        ‘User-Agent‘:‘-------‘#屬性,表示你用的什麼瀏覽器訪問   10 }11      #內部轉化:偽造上一次訪問記錄12  13 )

        -cookies    請求伺服器

放在headers裡面發過去的

以上為最重要的requests函數!!!!

     更多參數

 

session   拿到cookie 並拿到gpsd授予許可權

 1 import requests 2  3 session = requests.Session() 4  5 #####首先登陸任何頁面,擷取cookie 6 i1 = session.get(url = "https://dig.chouti.com/link/vote?linksId=21518036") 7 #####使用者登入,攜帶上一次的cookie,後台對cookie中的gpsd進行授權 8 i2 = session.post( 9     url = "http://dig.chouti.com/login",10     data= {11         ‘phone‘:‘xxx‘,12         ‘password‘:‘jshfkaj‘,13         ‘oneMonth‘:‘1‘14 15     }16 17 18 )19 i3 = session.post(20     url = ‘https://dig.chouti.com/link/vote?linksId=21518036‘21 )22 print(i3.text)

 

python爬蟲開發 (一)-------requests模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.