class UserInfo(object): def __init__(self): self.sessionid = rq.session() self.sessionid.cookies = cookielib.LWPCookieJar(filename="./userinfo.txt") self.urllogin = 'url' LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s" DATE_FORMAT = "%Y-%m-%d %H:%M:%S %p" logging.basicConfig(filename='../log/userinfo.log', level=logging.INFO, format=LOG_FORMAT, datefmt=DATE_FORMAT) def str2obj(self,s, s1=';', s2='='): li = s.split(s1) res = {} for kv in li: li2 = kv.split(s2) if len(li2) > 1: res[li2[0]] = li2[1] return res def queryLogdir(self,userid): logdir = 'url' data = {'searchText':userid,'pageNo':1,'pageSize':10,'containOrg':'false'} hds=''' Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 Accept-Encoding: gzip, deflate, br Accept-Language: zh-CN,zh;q=0.9 Cache-Control: max-age=0 Connection: keep-alive Cookie: cookiestr Host: dddd Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36 ''' res = self.sessionid.get(logdir, params=data,headers=self.str2obj(hds,'\n',': ')) userdict = json.loads(res.text) print(userdict) if __name__ == '__main__': userInfo = UserInfo() userInfo.queryLogdir('123456')
python使用cookie爬虫脚本
最新推荐文章于 2023-04-25 16:10:41 发布