分析post與json

尋找登陸的post地址

  • 在form表單中尋找action對應的url地址html

    • post的數據是input標籤中name的值做爲鍵,真正的用戶名密碼做爲值的字典,post的url地址就是action對應的url地址python

  • 抓包,尋找登陸的url地址web

    • 勾選perserve log按鈕,防止頁面跳轉找不到urlchrome

    • 尋找post數據,肯定參數json

      • 參數不會變,直接用,好比密碼不是動態加密的時候cookie

      • 參數會變dom

        • 參數在當前的響應中post

        • 經過js生成加密

定位想要的js

  • 選擇會觸發js時間的按鈕,點擊event listener,找到js的位置url

  • 經過chrome中的search all file來搜索url中關鍵字

  • 添加斷點的方式來查看js的操做,經過python來進行一樣的操做

安裝第三方模塊 (用於刷新網頁)

  • pip install retrying

  • 下載源碼解碼,進入解壓後的目錄,python setup.py install

  • ***.whl 安裝方法 pip install ***.whl

-----------------------------------------------------------

1、reqeusts.util.dict_from_cookiejar  把cookie對象轉化爲字典
1.1. requests.get(url,cookies={})
2、請求 SSL證書驗證
        response = requests.get("https://www.12306.cn/mormhweb/ ", verify=False)
3、設置超時
        response = requests.get(url,1)
4、配合狀態碼判斷是否請求成功
       assert response.status_code == 200
下面咱們經過一個例子總體來看一下以上4點的用法
# coding=utf-8
import requests
from retrying import retry

headers={"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"}

@retry(stop_max_attempt_number=3)
def _parse_url(url,method,data,proxies):
    print("*"*20)
    if method=="POST":
        response = requests.post(url,data=data,headers=headers,proxies=proxies)
    else:
        response = requests.get(url,headers=headers,timeout=3,proxies=proxies)
    assert  response.status_code == 200
    return response.content.decode()


def parse_url(url,method="GET",data=None,proxies={}):
    try:
        html_str = _parse_url(url,method,data,proxies)
    except:
        html_str = None

    return html_str

if __name__ == '__main__':
    url = "www.baidu.com"
    print(parse_url(url))

--------------------

In [1]: import requests

In [2]: response = requests.get('http://www.baidu.com')

In [3]: response.cookies
Out[3]: <RequestsCookieJar[Cookie(version=0, name='BDORZ', value='27315', port=N
one, port_specified=False, domain='.baidu.com', domain_specified=True, domain_in
itial_dot=True, path='/', path_specified=True, secure=False, expires=1544757805,
 discard=False, comment=None, comment_url=None, rest={}, rfc2109=False)]>

In [4]: requests.utils.dict_from_cookiejar(response.cookies)
Out[4]: {'BDORZ': '27315'}

In [5]: requests.utils.cookiejar_from_dict({'BDORZ': '27313'})
Out[5]: <RequestsCookieJar[Cookie(version=0, name='BDORZ', value='27313', port=N
one, port_specified=False, domain='', domain_specified=False, domain_initial_dot
=False, path='/', path_specified=True, secure=False, expires=None, discard=True,
 comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)]>

In [6]: requests.utils.quote('http://tieba.baidu.com/f?kw=李顏')
Out[6]: 'http%3A//tieba.baidu.com/f%3Fkw%3D%E6%9D%8E%E9%A2%9C'

In [7]: requests.utils.unquote('http%3A//tieba.baidu.com/f%3Fkw%3D%E6%9D%8E%E9%
   ...: A2%9C')
Out[7]: 'http://tieba.baidu.com/f?kw=李顏'

 

 

 

json使用注意點

  • json中的字符串都是雙引號引發來的

    • 若是不是雙引號

      • eval:能實現簡單的字符串和python類型的轉化

      • replace:把單引號替換爲雙引號

  • 往一個文件中寫入多個json串,再也不是一個json串,不能直接讀取

    • 一行寫一個json串,按照行來讀取

相關文章
相關標籤/搜索