headers、cookies处理

headers处理

def headers_to_dict(headers):
    row_headear = headers.split('\n')
    row_dict = dict()
    # headers_list = []
    for i in row_headear:

        if i == '':
            continue
        row = i.strip().split(':', 1)
        if len(row) == 0:
            continue
        if row[0] == '':
            continue
        row_dict[row[0].strip()] = row[1].strip()
    return row_dict

cookies处理

def cookie_to_dict(cookies):
    cookie_list = []
    row_cookie = cookies.split(';')
    for i in row_cookie:
        if i == '':
            continue
        row = i.strip().split('=', 1)

        row_dict = {row[0].strip(), row[1].strip()}
        cookie_list.append(row_dict)
    print(dict(cookie_list))

你可能感兴趣的:(爬虫,python语法,python)