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('
') for i in data1])
print(s)
print(sum(int(i) for i in s))