import requests #需要請求的目標地址 url='http://www.rrys2019.com/user/user' headers={'user-agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Mobile Safari/537.36'} #需要的登錄地址 login_url='http://www.rrys2019.com/User/Login/ajaxLogin' #如果需要爬蟲主動記錄cookie并且攜帶cookie,那么在使用使用requests之前先呼叫session方法 #并且使用session方法回傳的物件發送請求即可 rep=requests.session() #可以自動記錄cookie data={ 'account': ' ', 'password': ' ', 'remember': '1', 'url_back': 'http://www.rrys2019.com/user/user'} res=rep.post(url=login_url,headers=headers,data=https://www.cnblogs.com/luckiness/p/data) if res.status_code==200: #發起新的請求,獲取目標頁面 a=rep.get(url=url,headers=headers) #在登錄時自動攜帶cookie with open('aa.html','w',encoding='utf-8') as f: f.write(a.text)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/115628.html
標籤:Python
上一篇:Python 網路爬蟲實戰:爬取 B站《全職高手》20萬條評論資料
下一篇:手機爬蟲匯總
