码迷,mamicode.com
首页 > 编程语言 > 详细

python-spider 第七关

时间:2020-08-18 13:22:39      阅读:57      评论:0      收藏:0      [点我收藏+]

标签:app   turn   sum   wan   load   www   value   ant   print   


import json
from typing import Dict, List

import browsercookie
import requests
from requests.cookies import RequestsCookieJar

## init for classes

session = requests.session()
chrome_cookie = browsercookie.chrome()
s = []

url = "http://www.python-spider.com/api/challenge7"


def get_cookie_from_chrome(domain: str) -> List[Dict]:
    """
    :param domain: the cookies your want to get from.
    :return: a dict the contains cookies from the domain.
    """
    l = []
    for i in chrome_cookie:
        if domain in i.domain:
            l.append({‘name‘: i.name, "value": i.value})
    return l


def set_cookie(domain):
    """
    :param domain: the domain going to set
    :return: the instance of RequestsCookieJar contain the cookies of the domain you set
    """
    cookie_jar = RequestsCookieJar()
    cookies_list = get_cookie_from_chrome(domain=domain)                                                      
    for cookie in cookies_list:
        cookie_jar.set(cookie[‘name‘], cookie[‘value‘], domain=domain)

    return cookie_jar


if __name__ == ‘__main__‘:
    header = {
        ‘User-Agent‘: ‘Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36‘,
    }
    cookie_jar = set_cookie(‘www.python-spider.com‘)
    for i in range(1, 101):
        data = {"page": str(i)}
        before_response = session.post(‘http://www.python-spider.com/cityjson‘,headers=header,cookies=cookie_jar)
        response = session.post(url, headers=header, cookies=cookie_jar, data={"page": str(i)})
        print(response.text)
        data1 = json.loads(response.text)[‘data‘]

        s.extend([i[‘value‘].strip(‘\r‘) for i in data1])
    print(s)
    print(sum(int(i) for i in s))
 

python-spider 第七关

标签:app   turn   sum   wan   load   www   value   ant   print   

原文地址:https://www.cnblogs.com/ruhai/p/13510620.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!